Cod sursa(job #1746244)
| Utilizator | Data | 22 august 2016 22:23:13 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.45 kb |
#include <cstdio>
#include <algorithm>
using namespace std;
int gcd(int a,int b,int &x,int &y)
{
if(b==0)
{
x=1,y=0;
return a;
}
else
{
int X,Y,d;
d=gcd(b,a%b,X,Y);
x=Y,y=X-Y*(a/b);
return d;
}
}
int A,N,D,x,y;
int main()
{
freopen("inversmodular.in","r",stdin);
freopen("inversmodular.out","w",stdout);
scanf("%d%d",&A,&N);
D=gcd(A,N,x,y);
while(x<0)x+=N;
printf("%d",x%N);
return 0;
}