POJ 1995 快速幂 简单题

给出test,

对于每一个样例,输入M,H,然后是H行,每行是Ai,Bi

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

简单,直接快速幂即可。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 
 5 using namespace std;
 6 #define LL long long
 7 
 8 LL quick_pow(LL a,LL b,const LL mod)
 9 {
10     LL ret=1;
11     while(b>0)
12     {
13         if(b&1)
14             ret=ret*a%mod;
15         a=a*a%mod;
16         b>>=1;
17     }
18     return ret;
19 }
20 
21 LL solve()
22 {
23     int mod;
24     scanf("%d",&mod);
25     int n;
26     scanf("%d",&n);
27     LL ret=0;
28     for(int i=0;i<n;i++)
29     {
30         LL a,b;
31         scanf("%lld%lld",&a,&b);
32         ret=(ret+quick_pow(a%mod,b,mod))%mod;
33     }
34     return ret;
35 }
36 
37 int main()
38 {
39     int test;
40     scanf("%d",&test);
41     while(test--)
42     {
43         printf("%lld
",solve());
44     }
45     return 0;
46 }
View Code
原文地址:https://www.cnblogs.com/-maybe/p/4695825.html