HDU/HDOJ 3609 Up-up 2010多校联合17场ZSTU

 

Up-up

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 771    Accepted Submission(s): 212


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
   
   
   
   
3 2 3 3
 

Sample Output
   
   
   
   
27 97484987
 

Source
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 3609 Up-up 2010多校联合17场ZSTU)