A hard puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22701 Accepted Submission(s): 7957
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.
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
解题思路:把2 ~9 的 N次方打表找规律;
打表:
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int i , j;
for(i = 2; i <= 9 ; i++)
{
cout <<"a = " << i <<endl;
int k = i , sum = 1;
for(j = 1; j <= 16 ; j++)
{
sum *= k;
while(sum > 10)
{
sum %= 10;
}
cout << "b = " << j << "," << "a = " << sum <<'\t';
} cout << endl;
}
return 0;
}
/*
尾数表:
a = 2
b = 1,a = 2 b = 2,a = 4 b = 3,a = 8 b = 4,a = 6 b = 5,a = 2
b = 6,a = 4 b = 7,a = 8 b = 8,a = 6 b = 9,a = 2 b = 10,a = 4
b = 11,a = 8 b = 12,a = 6 b = 13,a = 2 b = 14,a = 4 b = 15,a = 8
b = 16,a = 6
a = 3
b = 1,a = 3 b = 2,a = 9 b = 3,a = 7 b = 4,a = 1 b = 5,a = 3
b = 6,a = 9 b = 7,a = 7 b = 8,a = 1 b = 9,a = 3 b = 10,a = 9
b = 11,a = 7 b = 12,a = 1 b = 13,a = 3 b = 14,a = 9 b = 15,a = 7
b = 16,a = 1
a = 4
b = 1,a = 4 b = 2,a = 6 b = 3,a = 4 b = 4,a = 6 b = 5,a = 4
b = 6,a = 6 b = 7,a = 4 b = 8,a = 6 b = 9,a = 4 b = 10,a = 6
b = 11,a = 4 b = 12,a = 6 b = 13,a = 4 b = 14,a = 6 b = 15,a = 4
b = 16,a = 6
a = 5
b = 1,a = 5 b = 2,a = 5 b = 3,a = 5 b = 4,a = 5 b = 5,a = 5
b = 6,a = 5 b = 7,a = 5 b = 8,a = 5 b = 9,a = 5 b = 10,a = 5
b = 11,a = 5 b = 12,a = 5 b = 13,a = 5 b = 14,a = 5 b = 15,a = 5
b = 16,a = 5
a = 6
b = 1,a = 6 b = 2,a = 6 b = 3,a = 6 b = 4,a = 6 b = 5,a = 6
b = 6,a = 6 b = 7,a = 6 b = 8,a = 6 b = 9,a = 6 b = 10,a = 6
b = 11,a = 6 b = 12,a = 6 b = 13,a = 6 b = 14,a = 6 b = 15,a = 6
b = 16,a = 6
a = 7
b = 1,a = 7 b = 2,a = 9 b = 3,a = 3 b = 4,a = 1 b = 5,a = 7
b = 6,a = 9 b = 7,a = 3 b = 8,a = 1 b = 9,a = 7 b = 10,a = 9
b = 11,a = 3 b = 12,a = 1 b = 13,a = 7 b = 14,a = 9 b = 15,a = 3
b = 16,a = 1
a = 8
b = 1,a = 8 b = 2,a = 4 b = 3,a = 2 b = 4,a = 6 b = 5,a = 8
b = 6,a = 4 b = 7,a = 2 b = 8,a = 6 b = 9,a = 8 b = 10,a = 4
b = 11,a = 2 b = 12,a = 6 b = 13,a = 8 b = 14,a = 4 b = 15,a = 2
b = 16,a = 6
a = 9
b = 1,a = 9 b = 2,a = 1 b = 3,a = 9 b = 4,a = 1 b = 5,a = 9
b = 6,a = 1 b = 7,a = 9 b = 8,a = 1 b = 9,a = 9 b = 10,a = 1
b = 11,a = 9 b = 12,a = 1 b = 13,a = 9 b = 14,a = 1 b = 15,a = 9
b = 16,a = 1
从表格可以发现 , 每乘 四次 , a的最后一位数都会循环 ;
最终代码:*/
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int num[10][4] = {{0, 0 , 0 , 0},//用二维数组把 0-9 的个尾储存起来;
{1 ,1 ,1 , 1 },
{6 ,2 ,4 , 8 },
{1 ,3 ,9 , 7 },
{6 , 4 , 6 , 4},
{5 , 5 , 5 , 5},
{6 , 6 , 6 , 6},
{1 ,7 ,9 , 3 },
{6 ,8 ,4 , 2 },
{1 ,9 ,1 , 9 }};
int a , b;
while(scanf("%d%d" ,&a , &b) != EOF)
{
printf("%d\n" , num[a%10][b%4]);
}
return 0;
}