Cod sursa(job #818745)
| Utilizator | Data | 17 noiembrie 2012 22:21:59 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.61 kb |
#include<cstdio>
using namespace std;
long long a,n,e=1,d,inv=1,i,N;
int main()
{
freopen("inversmodular.in","r",stdin);
freopen("inversmodular.out","w",stdout);
scanf("%lld%lld",&a,&n);
N=n;
if(!(n%2))
{
while(!(n%2)) {n/=2;e*=2;}
e/=2;
}
for(d=3;d*d<=n;d+=2)
{
if(!(n%d))
{
while(!(n%d)) {n/=d;e*=d;}
e/=d;e*=d-1;
}
}
if(n>1) e*=n-1;
e--;
/*for(i=0;(1<<i)<=e;i++)
{
if((1<<i)&e) inv=(inv*a)%N;
a=(a*a)%N;
}*/
printf("%lld\n",inv);
return 0;
}
