Pagini recente » Cod sursa (job #612650) | Cod sursa (job #2509150) | Cod sursa (job #2149735) | Cod sursa (job #1474753) | Cod sursa (job #1819923)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
long i,j,A,N,r=0;
long pew(long x,long y)
{
long p=1;
while(y!=0)
{
if(y%2!=0)
{
p=p*x;
p=p%N;
}
x=x*x;
y=y/2;
x=x%N;
}
return p;
}
int main()
{
f>>A>>N;
if(N==1 || N==2 || N==3 || N==5 || N==7 || (N%2!=0 && N%3!=0 && N%5!=0 && N%7!=0 && floor(sqrt(N))!=sqrt(N)))
{
r=N-2;
}
else
{
for(i=2;i<N;i++)
{
if(N%i!=0)
r++;
}
}
g<<pew(A,r);
}