Pagini recente » Cod sursa (job #2173951) | Cod sursa (job #1027502) | Cod sursa (job #844658) | Cod sursa (job #1449799) | Cod sursa (job #1316438)
#include <cstdio>
#include <cmath>
#define LL int
using namespace std;
int numprimes(int n)
{
int ans=n;
for(int i=2;i*i<=n;++i)
{
bool ok=false;
while(n%i==0)
{
n/=i;
ok=true;
}
if(ok)
{
ans/=i;
ans*=(i-1);
}
}
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;
}