H1N1's Problem(费马小定理+快速幂)

H1N1 like to solve acm problems.But they are very busy, one day they meet a problem. Given three intergers a,b,c, the task is to compute a^(b^c))%317000011. 1412, ziyuan and qu317058542 don't have time to solve it, so the turn to you for help.

输入

 

The first line contains an integer T which stands for the number of test cases. Each case consists of three integer a, b, c seperated by a space in a single line. 1 <= a,b,c <= 100000

输出

 

For each case, print a^(b^c)%317000011 in a single line.

1  a^b mod p 在b很大的时候可以先用b = b % (p-1)

2  a^(b^c) % p = a^( (b^c)%(p-1) )%p

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const ll Mod=317000011;
 5 ll t,a,b,c;
 6 
 7 ll Count(ll n,ll m,ll mod){
 8     ll res=1;
 9     n%=mod;
10     while(m){
11         if(m&1) res=res*n%mod;
12         m>>=1;
13         n=n%mod*n%mod;
14     }
15     return res%mod;
16 }
17 int main()
18 {
19     ios::sync_with_stdio(false);
20     cin>>t;
21     while(t--){
22         cin>>a>>b>>c;
23         cout << Count(a,Count(b,c,Mod-1),Mod) << endl;
24     }
25     return 0;
26 }
View Code
原文地址:https://www.cnblogs.com/qq-1585047819/p/11196892.html