Pagini recente » Cod sursa (job #3257702) | Cod sursa (job #2847353) | Cod sursa (job #421277) | Cod sursa (job #1011652)
#include <cstdio>
int n,m;
int calc(int nr)
{
int curent = nr;
for(int i = 2;i * i <= nr; i++)
{
if (!(nr % i))
{
while(!(nr % i))
nr /= i;
curent = (curent / i) * (i - 1);
}
}
if (nr != 1)
curent = curent / nr * (nr - 1);
return curent;
}
int main()
{
freopen("inversmodular.in","r",stdin);
freopen("inversmodular.out","w",stdout);
scanf("%d %d",&n,&m);
int power = calc(m) - 1;
int nr = n;
int curent = 1;
for(int p = 1;p <= power;p <<= 1)
{
if (p & power)
curent = (curent * nr) %m;
nr = (nr * nr) % m;
}
printf("%d\n",curent);
}