Pagini recente » Cod sursa (job #1360360) | Cod sursa (job #2125766) | Cod sursa (job #2305381) | Cod sursa (job #135882) | Cod sursa (job #2855605)
#include <fstream>
using namespace std;
ifstream fin ("inversmodular.in");
ofstream fout("inversmodular.out");
int Pow(int x, int y, int mod)
{
if (y==0) return 1;
if (y==1) return x%mod;
int p=Pow(x, y/2, mod);
if (y%2==0) return (1LL*p*p)%mod;
else return ((1LL*p*p)%mod*x)%mod;
}
int phi(int n)
{
int d, ct, fi=n;
d=2;
while (n!=1)
{
ct=0;
while (n%d==0) {n/=d, ct++;}
if (ct>0) fi=fi/d*(d-1);
if (d*d<n) d++;
else d=n;
}
return fi;
}
int main()
{
int a, n;
fin>>a>>n;
fout<<Pow(a, phi(n)-1, n);
return 0;
}