Pagini recente » Cod sursa (job #2884354) | Cod sursa (job #682363) | Cod sursa (job #2329415) | Cod sursa (job #2457962) | Cod sursa (job #1316439)
#include <cstdio>
#include <cmath>
#define LL int
using namespace std;
int numprimes(int n)
{
int lim=(int)sqrt((double)n);
int ans=n;
for(int i=2; i<=lim; i++)
{
bool ok=false;
while(n%i==0)
{
n/=i;
ok=true;
}
if(ok)
{
ans/=i;
ans*=(i-1);
lim=(int)sqrt((double)n);
}
}
if(n>1)ans=ans/n*(n-1);
return ans;
}
LL pow(LL base, LL exp, LL mod)
{
if(exp==0)
return 1;
else
{
int aux = pow(base,exp/2,mod);
if(exp&1)
return aux*aux%mod*base%mod;
else
return aux*aux%mod;
}
}
LL iv(LL a, LL b)
{
return pow(a,numprimes(b)-1,b);
}
int main()
{
freopen("inversmodular.in","r",stdin);
freopen("inversmodular.out","w",stdout);
LL a,n;
scanf("%d%d",&a,&n);
printf("%d\n",iv(a,n));
return 0;
}