hdu-1097 A hard puzzle

本文介绍了一种快速计算大数幂次方个位数的方法,通过简化运算并利用周期性规律,有效解决了计算效率问题。适用于数值范围在0到2^30之间的任意整数的幂次方个位数计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                                                                     A hard puzzle

Problem Description
lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.
this puzzle describes that: gave a and b,how to know the a^b's the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.
Input
There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)

Output
For each test case, you should output the a^b's last digit number.
Sample Input

7 66

8 800

 
Sample Output
9
6
题意:此题求a^b的个位数字。(0<a,b<=2^30)。数字很大,但要求的只是个位数字。怎么做呢?因为此题这么特殊的情况,0~9的幂的个位所呈的周期又很显然,周期最大也才4,还有很多周期就为1,如(0,1,5等),所以我采用了特殊方法。不管a是多少,只取他的个位数字来运算,算出0~9的幂的个位数字的周期,用b%这个周期,找到对应的答案。其实这个方法也不算怪吧,以前做此数学题就是这么做的呀,对吧?此题方法很多,用这个方法的人不知有几个呢?
#include<iostream>
using namespace std;
int main()
{
	int a,b,ans;
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		int x[4];  //事实证明,周期都在1~4
		int n;
		if(a>=10)
			a%=10; //既然只要求个位,干脆这样来简便运算
		switch(a)
		{
		case 0:ans=0;break;
		case 1:ans=1;break;
		case 2:x[0]=2;x[1]=4;x[2]=8;x[3]=6;n=b%4;if(n==0)	n=4;ans=x[n-1];break;  
			//思路一直很简单,但这里要注意; if(n==0)	n=4;ans=x[n-1]其实也不难理解吧....
		case 3:x[0]=3;x[1]=9;x[2]=7;x[3]=1;n=b%4;if(n==0)	n=4;ans=x[n-1];break;
		case 4:x[0]=4;x[1]=6;n=b%2;if(n==0)	n=2;ans=x[n-1];break;
		case 5:ans=5;break;
		case 6:ans=6;break;
		case 7:x[0]=7;x[1]=9;x[2]=3;x[3]=1;n=b%4;if(n==0)	n=4;ans=x[n-1];break;
		case 8:x[0]=8;x[1]=4;x[2]=2;x[3]=6;n=b%4;if(n==0)	n=4;ans=x[n-1];break;
		case 9:x[0]=9;x[1]=1;n=b%2;if(n==0)	n=2;ans=x[n-1];break;
		}
		printf("%d\n",ans);
	}
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值