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): 21925    Accepted Submission(s): 7696


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
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6 int main(void){
 7   long int a, b;
 8 #ifndef ONLINE_JUDGE
 9   freopen("1097.in", "r", stdin);
10 #endif
11   while (~scanf("%d%d", &a, &b)){
12     long int re[5];
13     re[0] = a%10;
14     for (int i = 1; i < 4; ++i){
15       re[i] = ((re[i-1]%10)*(a%10)) % 10;
16     }
17     int n = b%4; if (!n) n += 3; else n -= 1;
18     printf("%ld", re[n]);
19     printf("\n");
20   }
21   return 0;
22 }

很简单的题目,但是开始用最笨的方法做,不出所料,超时了,然后看了别人的代码,发现原来最大周期是4,所以问题就简单了,但还是卡了很久,

后来才发现可能是两个数字相乘可能会溢出,开始也想到这个了,但是没有那样写……唉,这么简单的题目做这么长时间。

posted on 2013-03-12 16:19  aries__liu  阅读(807)  评论(0编辑  收藏  举报