HDU/HDOJ 3609 Up-up 2010多校联合17场ZSTU
2011-09-09 22:02
381 查看
Up-up
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 771 Accepted Submission(s): 212
[align=left]Problem Description[/align]
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
[align=left]Input[/align]
A pair of a and k .a is a positive integer and fit in __int64 and 1<=k<=200
[align=left]Output[/align]
a↑↑k mod 100000000
[align=left]Sample Input[/align]
3 2 3 3
[align=left]Sample Output[/align]
27 97484987
[align=left]Source[/align]
2010 ACM-ICPC Multi-University Training Contest(17)——Host by ZSTU
这道题我WA了好久的说。。
先开始是5 2
10 3这两组数据过不了。。
后来改了之后还是一直WA。。
WA了10+之后我看了下discuss发现那个a和k居然可以都为0.。。。Orz。。我当时就崩溃了
不过题目也太不厚道了,都明明说了是正整数,居然还有0的情况。。。
这个题方法很简答
就是基于一个理论
a^b%c=a^(b%phi(c))%c
然后我是非递归写法和网上的主流写法貌似不一样。不过我认为要易懂一点
我的代码:
#include<stdio.h> typedef __int64 ll; ll mod=100000000; ll m[205]; ll eular(ll n) { ll i,ret=1; for(i=2;i*i<=n;i++) { if(n%i==0) { n=n/i; ret=ret*(i-1); while(n%i==0) { n=n/i; ret=ret*i; } } if(n==1) break; } if(n>1) ret=ret*(n-1); return ret; } void init() { ll i; m[1]=mod; for(i=2;i<=204;i++) m[i]=eular(m[i-1]); } ll power(ll a,ll b,ll c) { ll res=1; while(b) { if(b%2==1) res=res*a%c; a=(a%c)*(a%c)%c; b=b/2; } return res; } int main() { ll a,k,i,ans; init(); while(scanf("%I64d%I64d",&a,&k)!=EOF) { ans=a; if(a==0&&k%2==0) { printf("1\n"); continue; } for(i=k;i>=2;i--) { ans=ans%m[i]; if(ans==0) ans=m[i]; ans=power(a,ans,m[i-1]); } printf("%I64d\n",ans%mod); } return 0; }
相关文章推荐
- HDU/HDOJ 3519 矩阵二分幂 2010多校联合第九场
- HDU/HDOJ 3902 Swordsman 多校联合7
- HDU/HDOJ 3836 Equivalent Sets 多校联合1
- HDU/HDOJ 3875 Euclidean Algorithm 多校联合第四场
- HDU/HDOJ 3903 Trigonometric Function 多校联合7
- HDU/HDOJ 3908 Triple 多校联合7
- HDU/HDOJ 3944 DP? 多校联合11 电子科大 lucas定理
- hdu3530Subsequence【单调队列优化dp】2010多校联合
- HDOJ 5775 (2016多校联合训练 Training Contest 4) Bubble Sort
- HDU/HDOJ 3988 2011 WHU多校联合 Harry Potter and the Hide Story
- HDOJ多校联合第六场
- HDOJ 5792 (2016多校联合训练 Training Contest 5) World is Exploding
- HDOJ/HDU 3864 多校联合 D_num 数论
- HDOJ 5734 (2016多校联合训练 Training Contest 2) Acperience
- HDOJ 5791 (2016多校联合训练 Training Contest 5) Two
- HDU 3521 2010多校联合第9场 数学
- HDOJ 5723 (2016多校联合训练 Training Contest 1) Abandoned country
- HDOJ 5783 (2016多校联合训练 Training Contest 5) Divide the Sequence
- HDU/HDOJ R(N) 多校联合1
- HDU/HDOJ 3923 2011 BJTU多校联合 波利亚原理