Cod sursa(job #1645889)
| Utilizator | Data | 10 martie 2016 14:15:36 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.56 kb |
#include <cstdio>
using namespace std;
int n,a,d;
long long x,y;
void euclid(int a,int b,long long &x,long long &y,int d)
{
if(b == 0)
{
x=1;
y=0;
d=a;
return;
}
long long x0=0,y0=0;
euclid(b,a%b,x0,y0,d);
x=y0;
y=x0-y0*(a/b);
}
int main()
{
freopen("inversmodular.in","r",stdin);
freopen("inversmodular.out","w",stdout);
scanf("%d%d",&a,&n);
euclid(a,n,x,y,d);
if(x > 0) printf("%lld\n",x);
else
{
x=n+x%n;
printf("%lld\n",x);
}
return 0;
}
