Cod sursa(job #1357184)
Utilizator | Dinu Radu RaduGabriel2012 | Data | 23 februarie 2015 20:07:06 |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.45 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
int InvMod(int a,int b)
{ int i,k,c,x2,y2,x,y,r[35],aux=b;
while(b)
{ c=a%b; k++; r[k]=a/b;
a=b; b=c;
}
x=1; y=0;
for(i=k;i>=1;i--)
{ x2=y; y2=x-r[i]*y;
x=x2; y=y2;
}
while(x<0) x+=aux;
return x;
}
int main()
{ int a,n;
f>>a>>n;
g<<InvMod(a,n);
return 0;
}