Raising Modulo Numbers

For each assingnement there is the only one line of output. On this line, there is a number, the result of expression 

(A1B1+A2B2+ ... +AHBH)mod M.

Sample Input

3
16
4
2 3
3 4
4 5
5 6
36123
1
2374859 3029382
17
1
3 18132

Sample Output

2
13195
13

解:用快速幂的方法取模

#include <stdio.h>
int main(void)
{
long long n,k;
int t;
scanf("%d",&t);
for(int l=0;l<=t-1;l++)
{
scanf("%lld%lld",&n,&k);
long long a,b;
long long sum=0;
for(int i=1;i<=k;i++)
{
scanf("%lld%lld",&a,&b);
long long res=1;
while(b)
{
if(b%2==1) res=res*a%n;
a=a*a%n;
b/=2;
}
sum+=res%n;
}
printf("%lld\n",sum%n);
}
}

posted @ 2017-01-19 10:43  HaipaiAcm  阅读(161)  评论(0编辑  收藏  举报