Hust oj 1521 Marshal's Confusion III(整数快速幂)

Marshal's Confusion III
Time Limit: 3000 MS Memory Limit: 65536 K
Total Submit: 282(71 users) Total Accepted: 89(60 users) Rating: Special Judge: No
Description
Marshallike 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. so the turn to you for help. 
Input
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
Output
For each case, print a^(b^c)%317000011 in a single line.
Sample Input
2
1 1 1
2 2 2
Sample Output

1
16

Author

王勇

简单的整数快速幂,只不过从a^b mod c变成了a^b^c mod c,变形之后就是a^(b^c mod c-1)mod c,然后套模板

#include<iostream>
#include<cstring>

using namespace std;

const int mod=317000011;
long long  quickpow(long long   m , long long   n , long long   k)
{
    long long int ans = 1;
    while(n){
        if(n&1)
            ans = (ans * m) % k;
        n = n >> 1;
        m = (m * m) % k;
    }
    return ans;
}
int main()
{
    int n;
    long long int a,b,c;
    cin>>n;
    while(n--)
    {
        cin>>a>>b>>c;
        cout<<quickpow(a,quickpow(b,c,mod-1),mod)<<endl;;
    }
}


你可能感兴趣的:(快速幂)