Cod sursa(job #1673893)

Utilizator KOzarmOvidiu Badea KOzarm Data 4 aprilie 2016 10:45:38
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.47 kb
#include <fstream>

using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int a,n,x,y;
void inversmodular(int a,int b,int &x,int &y)
{
    if(b==0)
    {
        x=1;
        y=0;
    }
    else
    {
        inversmodular(b,a%b,x,y);
        int aux=x;
        x=y;
        y=aux-y*(a/b);
    }
}
int main()
{
    fin>>a>>n;
    inversmodular(a,n,x,y);
    while(x<0)
        x+=n;
    fout<<x;
    return 0;
}