Cod sursa(job #2764335)

Utilizator ps2001Silviu Popescu ps2001 Data 20 iulie 2021 13:48:48
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.52 kb
#include <iostream>
#include <fstream>

using namespace std;

int gcd(int a, int b, int &x, int &y) {
    if (b==0) {
        x = 1;
        y = 0;
        return a;
    }

    int x0, y0, d;
    d = gcd(b, a%b, x0, y0);

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

    return d;
}

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

    int a, n, x, y;
    fin>>a>>n;

    int d = gcd(a, n, x, y);
    while (x < 0) {
        x+=n;
    }
    int inv = x / d;
    fout<<inv<<'\n';
    return 0;
}