hdu 1097 A hard puzzle
March 12, 2013
Hdu
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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(void){
long int a, b;
#ifndef ONLINE_JUDGE
freopen("1097.in", "r", stdin);
#endif
while (~scanf("%d%d", &a, &b)){
long int re[5];
re[0] = a%10;
for (int i = 1; i < 4; ++i){
re[i] = ((re[i-1]%10)*(a%10)) % 10;
}
int n = b%4; if (!n) n += 3; else n -= 1;
printf("%ld", re[n]);
printf("\n");
}
return 0;
}
很简单的题目,但是开始用最笨的方法做,不出所料,超时了,然后看了别人的代码,发现原来最大周期是4,所以问题就简单了,但还是卡了很久,
后来才发现可能是两个数字相乘可能会溢出,开始也想到这个了,但是没有那样写……唉,这么简单的题目做这么长时间。