HDU 1097 a hard puzzle
A hard puzzle
Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24457 Accepted Submission(s): 8676
Problem Description
lcy gives a hardpuzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know thea^b.everybody objects to this BT problem,so lcy makes the problem easier thanbegin.
this puzzle describes that: gave a and b,how to know the a^b's the last digit number.Buteverybody is too lazy to slove this problem,so they remit to you who is wise.
Input
There are mutipletest cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)
Output
For each testcase, you should output the a^b's last digit number.
Sample Input
7 66
8 800
Sample Output
9
6
很容易发现 a^b 的规律,个位数每四个一循环,有的虽然周期是2,但是4个一定是一个周期。
2: 2 4 8 16 32 64
3: 3 9 27 81 243 729
4: 4 16 64 256 1024
另外,我开始没有发现题目说a ,b 都 大于零。
因此,我的代码支持 0 ^ 0 =1(貌似在哪见过), 0 ^ 1=0 , 1 ^ 0=1;
#include "string"
#include "iostream"
using namespace std;
int main( )
{int m,n,s,i,tem;
while(cin>>m>>n)
{
if( m==1||n==0 )
s=1;
else if( m%10==0 )
s=0;
else if( n==1 )
s=m%10;
else{ tem=1;
m=m%10;//只保留个位数
if( n%4==0 )
{
tem=m*m*m*m;
}
else
for( i=0;i<n%4;i++ )
tem*=m;
s=tem%10;
}
cout<<s<<endl;
}
return 0;
}

浙公网安备 33010602011771号