Description |
Marshal 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. 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 |
Sample Output |
1 |
欧拉定理表明,若n,p为正整数,且n,p互质,则a^f(p)=1(mod p)。
而费马小定理是欧拉定理的一个特殊情况,其内容为: 假如p是质数,且Gcd(a,p)=1,那么 a(p-1) ≡1(mod p)。即:假如a是整数,p是质数,且a,p互质(即两者只有一个公约数1),那么a的(p-1)次方除以p的余数恒等于1。
对于b^c,可以把b^c分解成m*p+r 的形式,则a^(b^c)就变成了a^( m *p+r )
继续变成a^(m*p) * a^r 。当它整体对p取模的时候,已知a^( m*p)=(a^m)*p 和1同余,那么只要求出a^r就行。
已知f(p)是1到与p互质的数的个数,应该为p-1,所以b^c应该对p-1取余,剩下的则为r,然后再算a^r 对p取余。
#include <iostream>
using namespace std;
const int mod=317000011;
long long mul(long long m,long long n,int k)
{
long long res=1;
while(n)
{
if(n&1)
res=(res*m)%k;
m=(m*m)%k;
n=n/2;
}
return res;
}
int main()
{
int T;
cin>>T;
while(T--)
{
int a,b,c;
cin>>a>>b>>c;
cout<<mul(a,mul(b,c,mod-1),mod)<<endl;
}
return 0;
}