H1N1's Problem
时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte
总提交: 10 测试通过: 3
描述
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))17000011. 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)17000011 in a single line.
样例输入
2
1 1 1
2 2 2
样例输出
1
16
题意: 非常简单的题意. 求 a^(b^c) % 317000011
解题思路:
1. 数论题先看规模的大小. (1,10000). 规模不大.
2. 用费马定理吧. 二分法求解更快.
3. 先要将原公式化简下.
分析: a^(b^c) % p = a^( (b^c)%(p-1) )%p
代码:
#include <stdio.h>
#include <iostream>
using namespace std;
const __int64 MAX = (1<<31);
__int64 pow_mod(__int64 a,__int64 n,__int64 m)
{
if(n == 0)
return 1;
__int64 x = pow_mod(a,n/2,m);
__int64 ans = (__int64) x * x % m;
if(n % 2 == 1) ans = ans * a % m;
return ans;
}
int main()
{
__int64 a , b , c;
__int64 d = 317000011;
int n;
scanf("%d",&n);
while(n--)
{
scanf("%I64d %I64d %I64d",&a,&b,&c);
__int64 cur = pow_mod(b,c,d-1);
printf("%I64d\n",pow_mod(a,cur,d));
}
return 0;
}