POJ 1995 Raising Modulo Numbers

时间:2020-12-03 12:41:33

快速幂取模

#include<cstdio>

int mod_exp(int a, int b, int c)
{
int res, t;
res = % c;
t = a % c;
while (b)
{
if (b & ) res = res * t % c;
t = t * t % c;
b >>= ;
}
return res;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int m,h;
scanf("%d%d",&m,&h);
int ans=;
for(int i=; i<=h; i++)
{
int a,b;
scanf("%d%d",&a,&b);
ans=(ans+mod_exp(a,b,m))%m;
}
printf("%d\n",ans);
}
return ;
}