Raising Modulo Numbers POJ 1995 (fast power template)

The original title

Topic Link

Topic analysis

Fast power template title, according to a set of questions intended to fast modulo Mi, then the answer plus side mold can be.

Code

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <utility>
 6 #include <ctime>
 7 #include <cmath>
 8 #include <cstring>
 9 #include <string>
10 #include <stack>
11 #include <queue>
12 #include <vector>
13 #include <set>
14 #include <map>
15 
16 using namespace std;
17 typedef long long LL;
18 const int INF_INT=0x3f3f3f3f;
19 const LL INF_LL=0x3f3f3f3f3f3f3f3f;
20 
21 LL q_power(LL a,LL b,LL mod)
22 {
23     LL res=1;
24     a%=mod;
25     while(b)
26     {
27         if(b&1) res=(res*a)%mod;
28         a=(a*a)%mod;
29         b>>=1;
30     }
31     return res;
32 }
33 
34 int main()
35 {
36 //    freopen("black.in","r",stdin);
37 //    freopen("black.out","w",stdout);
38     int t;
39     scanf("%d",&t);
40     while(t--)
41     {
42         int m,h;
43         scanf("%d %d",&m,&h);
44         LL res=0;
45         for(int i=0;i<h;i++)
46         {
47             LL a,b;
48             scanf("%lld %lld",&a,&b);
49             res=(res+q_power(a,b,m))%m;
50         }
51         printf("%lld\n",res);
52     }
53     return 0;
54 }

 

Guess you like

Origin www.cnblogs.com/VBEL/p/11444673.html