Cod sursa(job #1999367)

Utilizator dumitrescu_andreiDumitrescu Andrei dumitrescu_andrei Data 11 iulie 2017 00:06:01
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.53 kb
#include <fstream>

using namespace std;

int A,N,x,y,c,d;

int euclid(int a, int b, int &x, int &y){
    if(b == 0){
        x = 1;
        y = 0;
        return a;
    }
    int x0,y0;
    d = euclid(b, a % b, x0, y0);
    x = y0;
    y = x0 - (a / b) * y0;
    return d;
}
int main()
{
    ifstream f("inversmodular.in");
    ofstream g("inversmodular.out");
    f >> A >> N;
    ///ax - qn = 1;
    d = euclid(A,N,x,y);
    x = x * (1/d);
    if(x < 0)
        x = N + x % N;
    g << x;
    return 0;
}