Cod sursa(job #3196230)

Utilizator MagicantPlusIuoras Andrei MagicantPlus Data 23 ianuarie 2024 10:47:48
Problema Invers modular Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");

// #define cin fin
// #define cout fout

long long a, n;

void euclid(long long a, long long b, long long& x, long long& y);

int main()
{
    long long x, y;

    cin >> a >> n;

    euclid(a, n, x, y);

    while(x < 0)
    {
        x += n;
    }

    cout << x % n;

    return 0;
}

void euclid(long long a, long long b, long long& x, long long& y)
{
    if(b == 0)
    {
        y = 0;
        x = 1;
        return;
    }

    long long x1, y1;

    euclid(b, a % b, x1, y1);

    x = y1;
    y = x1 - a / b * y1;

    // x %= n;
    // y %= n;
}