Cod sursa(job #1248092)

Utilizator gabrieligabrieli gabrieli Data 24 octombrie 2014 17:17:31
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 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);
    long long r = a % m;
    fout << (m + r) % m << endl;
        
    return 0;
}