Cod sursa(job #2045079)
| Utilizator | Data | 21 octombrie 2017 19:30:43 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 60 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.61 kb |
#include <fstream>
using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
int main()
{
long long a,cn,n,x=1,i,p=2,e,s,phi;
in>>a>>n;
s=1;
phi=n;
cn=n;
e=0;
while(p*p<=n && n>1)
{
while(n%p==0)
{
e++;
n=n/p;
}
if(e)
phi=phi/p*(p-1);
p++;
e=0;
}
if(n>1)
phi=phi/n*(n-1);
phi--;
p=1;
while(phi)
{
if(phi&1)
p=(p*a)%n;
a=(a*a)%n;
phi=phi/2;
}
out<<p;
return 0;
}
