HDU 3609Up-up(层层递推降幂+蛋疼的特判)
来源:互联网 发布:杭州初级程序员薪资 编辑:程序博客网 时间:2024/05/22 14:52
Up-up
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1485 Accepted Submission(s): 420
Problem Description
The Up-up of a number a by a positive integer b, denoted by a↑↑b, is recursively defined by:
a↑↑1 = a,
a↑↑(k+1) = a (a↑↑k)
Thus we have e.g. 3↑↑2 = 33 = 27, hence 3↑↑3 = 327= 7625597484987 and 3↑↑4 is roughly 103.6383346400240996*10^12
The problem is give you a pair of a and k,you must calculate a↑↑k ,the result may be large you can output the answer mod 100000000 instead
a↑↑1 = a,
a↑↑(k+1) = a (a↑↑k)
Thus we have e.g. 3↑↑2 = 33 = 27, hence 3↑↑3 = 327= 7625597484987 and 3↑↑4 is roughly 103.6383346400240996*10^12
The problem is give you a pair of a and k,you must calculate a↑↑k ,the result may be large you can output the answer mod 100000000 instead
Input
A pair of a and k .a is a positive integer and fit in __int64 and 1<=k<=200
Output
a↑↑k mod 100000000
Sample Input
3 23 3
Sample Output
2797484987
题目大意:给你一个n一个k,问你n^(n^(n^(n...))总共有k个n这样的。问结果模上10^8是多少?看到了会立马想到降幂公式,但是却没有想到是层层递推的。每一层有自己的phi,然后模上相应层所对应的mod。自己在纸上画画可以把思路理一下。
解题思路:自己开始没注意到base需要%mod预处理一下,结果老是TLE。降幂公式:A^x = A^(x % Phi(C) + Phi(C)) (mod C)不过用这个公式需要一个条件 x>=Phi(C).但是这个题我却根本没有判断X>=phi(C),现在对这个降幂公式也只能将就着用了。具体思路见代码。还有就是0的奇数次方是0,0^偶数次方是1.这就让我有点不淡定了。。不过数据还真的就是这样,需要特判。
题目地址:Up-up
AC代码:
#include<iostream>#include<cstring>#include<cstdio>#include<cmath>using namespace std;__int64 a;int mo=100000000;int el[205];int geteuler(int n) //得到欧拉值{ int m=sqrt(n+0.5),ans=n,i; for(i=2;i<=m;i++) if(n%i==0) { ans=ans/i*(i-1); while(n%i==0) n/=i; } if(n>1) ans=ans/n*(n-1); return ans;}__int64 pow(__int64 base,__int64 p,__int64 mod) //快速幂取模{ __int64 ans=1; base%=mod; //预处理 while(p) { if(p&1) ans=(ans*base)%mod; base=(base*base)%mod; p>>=1; } return ans;}int main(){ int i,k; el[1]=mo; for(i=2;i<=200;i++) el[i]=geteuler(el[i-1]); while(~scanf("%I64d%d",&a,&k)) { __int64 res=a; if(a==0&&k%2==0) {puts("1");continue;} if(a==0) {puts("0");continue;} //特判 while(k>1) { res=res%el[k]+el[k]; //if(res==0) res=el[k]; res=pow(a,res,el[k-1]); k--; } printf("%I64d\n",res%mo); } return 0;}
- HDU 3609Up-up(层层递推降幂+蛋疼的特判)
- hdu 3609 Up-up
- HDU 3609 Up-up(高次降幂)
- up
- UP
- up
- up!
- up
- UP
- Up
- up
- up
- up
- poj1564Sum It Up(dfs+判重)
- HDU1258 Sum It Up 【DFS】+【判重】
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
- hdu 4612 Warm up 有重边的tarjan
- hdu 3954 Level up
- [leetcode] Binary Tree Maximum Path Sum
- Intel Fortran Compiler
- EasyBCD的使用心得
- MFC下测量程序执行时间
- 解决:Google代码achartengine曲线代码报错问题
- HDU 3609Up-up(层层递推降幂+蛋疼的特判)
- hdu 1074 Doing Homework(状态压缩DP)
- AChartEngine小Demo,肯定能运行的。
- uva 12301 - An Angular Puzzle
- 我的Android进阶之旅------>Android基于HTTP协议的多线程断点下载器的实现
- 留给自己
- 1一站式C语法深究
- JFreeChart使用
- 为什么游戏叫第九艺术?