Pagini recente » Cod sursa (job #1426607) | Cod sursa (job #3214416) | Cod sursa (job #775190) | Cod sursa (job #1901572) | Cod sursa (job #3167894)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int A,N;
int po(int a, int b)
{
if(b==0) return 1;
if(b%2==1)
return a*po(a,b-1)%N;
int p=po(a,b/2)%N;
return (p*p)%N;
}
int phi(int n)
{
int r=n,d=2;
while(n>1)
{
if(n%d==0)
{
r=r/d*(d-1);
while(n%d==0)
n/=d;
}
d++;
if(d*d>n)
d=n;
}
return r;
}
int main()
{
fin>>A>>N;
int ph=phi(N)-1;
fout<<po(A,ph);
return 0;
}