ACM: 数论 台州OJ 3151 (久违的数…

 

                                                  H1N1's Problem 分享至QQ空间

时间限制(普通/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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值