哈理工OJ 1251 【带坑的快速幂】【好题】
来源:互联网 发布:三国志9pk优化伴侣设置 编辑:程序博客网 时间:2024/05/29 15:52
Marshal's Confusion IIITime Limit: 3000 MSMemory Limit: 65536 KTotal Submit: 251(67 users)Total Accepted: 81(56 users)Rating: Special Judge: NoDescriptionMarshallike 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. InputThe 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 <= 100000OutputFor each case, print a^(b^c)%317000011 in a single line.Sample Input2
1 1 1
2 2 2Sample Output
1 1 1
2 2 2Sample Output
1
16
这里思路很简单,对b,c进行一次快速幂求余,然后得到一个值假设为ans,然后a,ans再来一次快速幂求余搞定整个题~。然后我就开开心心的用这个方法去做,怎么交怎么WA,怎么写怎么改都是WA 。这个时候内心近乎于崩溃。
这个时候坑点出来了:如果b^c得到的值是,317000011,我用快速幂求余的方法,得到的值是0啊,然后a^0永远都是1啊,那不对啊。所以这个时候我开始用草纸举栗子,发现有一个结论,如果mod【这里题目给的是317000011】是个奇数,那么就有这样的一个结论:如果b^c==mod,那么a^(b^c)==a;
然后排除了这个坑,我们就能开开森森的做题啦~
#include<stdio.h>#include<string.h>using namespace std;const long long int mod=317000011;long long int kuaisumi(long long int a,long long int b,int mod){ long long int tmp=a%mod; long long int ans=1; int n=b; while(n) { if(n%2==1) { ans=(ans*tmp)%mod; n-=1; } else { tmp=(tmp*tmp)%mod; n/=2; } } return ans;}int main(){ int t; scanf("%d",&t); while(t--) { long long int a,b,c; scanf("%lld%lld%lld",&a,&b,&c); long long int output=kuaisumi(a,kuaisumi(b,c,mod-1),mod);//这里mod-1就能AC了~ printf("%lld\n",output); }}
0 0
- 哈理工OJ 1251 【带坑的快速幂】【好题】
- 哈理工 OJ 1950--QuickPow(快速幂)
- hrbust/哈理工oj 1592 六芒星【矩阵快速幂】
- 哈理工OJ 2240 土豪的时代【带权并查集】
- hrbust 哈理工oj 1834 括号匹配【好题】【floyd+dp】
- 哈理工 OJ 2215 Angle(简单的思维题)
- hrbust/哈理工oj 1126 Final Destination II【矩阵快速幂+递推】
- hrbust 1430 哈理工oj 1430 神秘植物【递推+矩阵快速幂】
- hrbust/哈理工oj 1787 New Fibonacci Number【欧拉降幂+矩阵快速幂】
- 哈理工OJ 1430 神秘植物(矩阵快速幂+矩阵构造)
- 哈理工 oj 2189 节点的连接
- Hrbust oj/哈理工 oj 1216数的划分
- 哈理工 1251 Marshal's Confusion III(太坑快速幂)
- 哈理工OJ 2271 从前的苹果树(简单的规律题)
- 哈理工OJ 2179(深搜)
- 哈理工 oj 1161 Leyni
- 哈理工OJ 1692【水题】
- 哈理工oj 2317 Game
- Android 项目在Eclipse中的目录结构
- Xcode 之自己编译静态库
- 利用wordpress数据库写API,附加文章redis缓存[菜鸡简单粗暴版]
- Java设计模式(五)----原型模式
- 37 sensor kit. Arduino
- 哈理工OJ 1251 【带坑的快速幂】【好题】
- MYSQL UPDATE不支持子查询的处理方式
- js调用函数的方法
- iPhone/iOS开启个人热点的纵向适配小结
- SEO原创文章制作器
- arduino sensors
- NSNotificationCenter 用法注意点
- js定义函数的三种方法
- 动画浅析-CAAnimation和CATransition