Pagini recente » Cod sursa (job #889102) | Cod sursa (job #2829089) | Cod sursa (job #1491769) | Cod sursa (job #2948166) | Cod sursa (job #1649509)
#include <stdio.h>
#define LL long long
using namespace std;
long long N, M;
long long phi(long long n){
long long copy = n;
for(long long i=2; i*i<=n; i++)
if(n%i==0)
{
while(n%i==0) n=n/i;
copy = (copy/i) * (i-1);
}
if(n!=1) copy = (copy/n)*(n-1);
return copy;
}
int main()
{
freopen("inversmodular.in", "r", stdin);
freopen("inversmodular.out", "w", stdout);
scanf("%lld %lld", &N, &M);
LL put = phi(M) - 1;
LL nr = N;
LL crt = 1;
for(LL p = 1;p <= put;p <<= 1)
{
if (p & put) crt = (crt * nr) % M;
nr = (nr * nr) % M;
}
printf("%lld\n",crt);
return 0;
}