(A1B1+A2B2+ ... +AHBH)mod M.
3 16 4 2 3 3 4 4 5 5 6 36123 1 2374859 3029382 17 1 3 18132
2 13195 13
结合律 |
((a+b) mod p + c)mod p = (a + (b+c) mod p) mod p((a*b) mod p * c)mod p = (a * (b*c) mod p) mod p |
交换律 |
(a + b) mod p = (b+a) mod p(a × b) mod p = (b × a) mod p |
分配律 |
((a +b)mod p × c) mod p = ((a × c) mod p + (b × c) mod p) mod p(a×b) mod c=(a mod c * b mod c) mod c(a+b) mod c=(a mod c+ b mod c) mod c(a-b) mod c=(a mod c- b mod c) mod c |
1 #include<cstdio> 2 __int64 f(__int64 a,__int64 b,__int64 m) 3 { 4 __int64 sun=1; 5 while(b) 6 { 7 if(b % 2 != 0) 8 { 9 sun=sun*a%m; 10 } 11 a=a*a%m; 12 b/=2; 13 } 14 return sun%m; 15 } 16 int main() 17 { 18 int t,i; 19 __int64 n,m,sum,a,b; 20 scanf("%d",&t); 21 while(t--) 22 { 23 sum=0; 24 scanf("%I64d",&m); 25 scanf("%I64d",&n); 26 for(i = 0 ; i < n; i++) 27 { 28 scanf("%I64d %I64d",&a,&b); 29 sum+=f(a,b,m); 30 // printf("---%d--\n",f(a,b,m)); 31 } 32 printf("%I64d\n",sum%m); 33 } 34 }
(快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M
原文:http://www.cnblogs.com/yexiaozi/p/5698426.html