Cod sursa(job #1669413)

Utilizator anav23Ana Vasiliu anav23 Data 30 martie 2016 18:12:07
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.46 kb
#include <iostream>
#include <fstream>
using namespace std;

void cmmdc(int a, int n, int &x, int &y) {
    if(!n)
        x=1,y=0;
    else {
        int x0,y0;
        cmmdc(n,a%n,x0,y0);
        x=y0;
        y=x0-y0*(a/n);
    }
}

int main()
{
    ifstream fin("inversmodular.in");
    ofstream fout("inversmodular.out");
    int a,n,x,y;
    fin>>a>>n;
    cmmdc(a,n,x,y);
    if(x<=0)
        x=n+x%n;
    fout<<x<<'\n';
    return 0;
}