超级快速幂
Time Limit: 3000/1000 MS(Java/Others) Memory Limit:65536/65536 K (Java/Others)
Description
请计算:
a^(b^c)mod(1e9+7)
Input
多组数据,第一行为一个整数T,代表数据组数,接下来每行三个整数a,b,c(1<=a,b,c<=1,000,000,000)
Output
对于每组数据,输出一行代表计算结果
Sample Input
2
1 2 3
2 3 2
Sample Output
1
512
思路:根据费马小定理a^(p-1) =1mod(p),p为质数,当我们计算a^x%p时,考虑化成a^[t(p-1)+k]%p,即a^k%p,那么本题只需要计算a^[b^c%(p-1)]%p即可。# include <iostream>
# include <cstdio>
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
LL qmod(LL a, LL b, LL mo)
{
LL ans = 1, pow = a;
for(;b;b>>=1)
{
if(b&1) ans = ans*pow%mo;
pow = pow*pow%mo;
}
return ans;
}
int main()
{
int t;
LL a, b, c;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%lld",&a,&b,&c);
LL pow = qmod(b, c, mod-1);
printf("%lld\n",qmod(a, pow, mod));
}
return 0;
}