Cod sursa(job #2930418)

Utilizator DKMKDMatei Filibiu DKMKD Data 28 octombrie 2022 13:31:32
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <fstream>

using namespace std;

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

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

int main(){
    int a, n, x, y, d;
    fin >> a >> n;
    cmmdc(a, n, x, y, d);
    if (d == 1){
        while (x < 0)
            x += n;
        fout << x;
    }
    return 0;
}