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): 35099    Accepted Submission(s): 12610


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
 

 

Author
eddy
 

 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1170 1164 1087 1032 1062 
 
和以前做的一道题几乎一样,上一次是求N^N的个位数,这次是求a^b的个位数,不过过程其实是一样的。。
 
题意:求a^b的个位数。
 
附上代码:
 
 1 #include <iostream>
 2 using namespace std;
 3 int s[5];
 4 void add(int t)
 5 {
 6     int i,k=1;
 7     for(i=1; i<=4; i++) //通过打表找规律,发现a^b的个位数字4个一循环,而且只需要保存个位数
 8     {
 9         k=k*t%10;
10         s[i]=k;
11     }
12 }
13 int main()
14 {
15     int a,b,i,k;
16     while(cin>>a>>b)
17     {
18         if(b==0)
19         {
20             cout<<1<<endl;
21             continue ;
22         }
23         a=a%10;
24         add(a);
25         k=b%4;
26         if(k==0)
27             cout<<s[4]<<endl;
28         else
29             cout<<s[k]<<endl;
30     }
31     return 0;
32 }

 

posted @ 2015-09-17 16:51  lucky_少哖  阅读(137)  评论(0编辑  收藏  举报