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 = 3
3 = 27, hence 3↑↑3 = 3
27= 7625597484987 and 3↑↑4 is roughly 10
3.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
Sample Output
题目大意:
给你一个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;
}