Cod sursa(job #2900665)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 11 mai 2022 20:11:41
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <fstream>

using namespace std;

#define int long long

const string filename = "inversmodular";
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");

///x0 * b + y0 * (a - (a/b) * b) = x * a + y * b

void big_euclid(int a, int b, int &x, int &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return;
    }
    big_euclid(b, a % b, x, y);
    int x0 = x, y0 = y;
    x = y0;
    y = x0 - y0 * (a / b);
}

signed main()
{
    int n, a, x, y;
    fin >> a >> n;
    big_euclid(n, a, x, y);
    while(y < 0)
        y += n;
    fout << y;
    return 0;
}