NYOJ-A^B Problem

A^B Problem

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 2
描述
Give you two numbers a and b,how to know the a^b's the last digit number.It looks so easy,but everybody is too lazy to slove this problem,so they remit to you who is wise.
输入
There are mutiple test cases. Each test cases consists of two numbers a and b(0<=a,b<2^30)
输出
For each test case, you should output the a^b's last digit number.
样例输入
7 66
8 800
样例输出
9
6
提示
There is no such case in which a = 0 && b = 0。
来源
hdu
上传者

ACM_丁国强


代码:

#include
#include
int main()
{
	int A,B,t,d,i,j,f[110];
	while(~scanf("%d%d",&A,&B))
	{
	    if(B==0)
	    {
	    	printf("1\n");
	    	continue;
	    }
	    
	    memset(f,0,sizeof(f));
		f[1]=A%10;
		for(i=2;i<110;++i)
		{
			d=f[i-1]*f[1];
			f[i]=d%10;
			if(f[i]==f[1])
			break;
		}
		int n=B%(i-1);
		if(!n)
		printf("%d\n",f[i-1]);
		else
		printf("%d\n",f[n]);
	}
	
	return 0;
}

#include
int f(int a,int b,int c)
{
	if(b==0)
	return 1;
	if(b==1)
	return a%c;
	int ret=f(a,b/2,c);
	ret=ret*ret%10;
	if(b&1)
	ret=ret*a%c;
	return ret;
}
int main() 
{
	int a,b;
	while(~scanf("%d%d",&a,&b))
	{
		printf("%d\n",f(a,b,10));
	}
	return 0;
}


你可能感兴趣的:(大数)