Cod sursa(job #1679703)

Utilizator razvandRazvan Dumitru razvand Data 8 aprilie 2016 10:24:55
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.49 kb
#include <iostream>
#include <fstream>

using namespace std;

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

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

int main() {

    int a,n,inv,y;
    in >> a >> n;
    gcd(a, n, inv, y);
    if(inv <= 0)
        inv = n + inv%n;
    out << inv;

    return 0;
}