原题链接
Description
A^B % P is a very interesting problem. Here a more bigger problem needs you to solve.
((((AB[0])B[1])…)B[n-1])%P
In which B[i]=B[i-1]^2-1( i > 0 ), P=1e9+7
Input
The input consists of several test cases.
The first line of the input contains a single integer T (0 < T ≤ 20), the number of test cases.
Then Followed by T lines, each line gives a test case which contains three integers A, n and B0.
0<A<2^31 0<n<=10000 1<B[0]<2^31
Output
For each test case, output an integer representing the result of (((AB[0])B[1])…)B[n-1]%P
Sample Input
2
3 1 2
2 2 2
Sample Output
9
64
注释请看代码
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
//const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
//const DB EPS = 1e-9;
//const DB OO = 1e20;
//const DB PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=10000 + 10;
//typedef long long ll;
//快速a^b%c计算
//特别需要注意的是如果c是一个素数,那么a^b%c==a^( b%(c-1) )%c,这就是费马小定理
long long fastpow(long long a,long long b){
long long res=1;
while(b){
if(b&1) res*=a;
res%=MOD;
a=(a*a)%MOD;
b=(b>>1);
}
return res;
}
int main(){
int T;
cin>>T;
while(T--){
long long a,n,BI;
cin >> a >> n >> BI;
long long pow=BI;
n--;
while(n--){
BI=(BI*BI-1)%(MOD-1);
pow=(pow*BI)%(MOD-1);
}
cout << fastpow(a,pow) << endl;
}
return 0;
}