Cod sursa(job #3122899)

Utilizator nusmaibunkeleviprofesor cicalescu nusmaibunkelevi Data 21 aprilie 2023 07:56:46
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.47 kb
#include <fstream>

using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");

void inv_mod(int a, int b, int &x , int &y){
    if (!b){
        x = 1, y = 0;
    } else {
        int x1, y1;
        inv_mod(b, a % b, x1, y1);
        x = y1;
        y = x1 - a / b * y1;
    }
}

int main()
{
    int a, b, x, y;
    in >> a >> b;
    inv_mod(a, b, x, y);
    while(x < 0)
        x += b;
    out << x;
    return 0;
}