Cod sursa(job #1248091)

Utilizator gabrieligabrieli gabrieli Data 24 octombrie 2014 17:13:53
Problema Invers modular Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.49 kb
#include <fstream>

using namespace std;

int gcd_ext(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    
    int d = gcd_ext(b, a%b, x, y);
    
    int t = x;
    x = y;
    y = t - (a / b) * y;
    
    return d;
}

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

    int x, m, a, b;
    fin >> x >> m;
    
    gcd_ext(x, m, a, b);
    fout << (m + a % m) % m << endl;
        
    return 0;
}