Categories
不学无术

HDOJ-1097 A hard puzzle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23989    Accepted Submission(s): 8466

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

==========================================
死搞肯定是要超时的….
其实一共9个数字,乘方都是有规律的..写的丑陋了点,不过能用就好^^

#include 
#include 
using namespace std;
const int BUFFER_LENGTH = 31;
const int pattern_size[] = {1, 1, 4, 4, 2, 1, 1, 4, 4, 2};
const int pattern_0[] = {0};
const int pattern_1[] = {1};
const int pattern_2[] = {2, 4, 8, 6};
const int pattern_3[] = {3, 9, 7, 1};
const int pattern_4[] = {4, 6};
const int pattern_5[] = {5};
const int pattern_6[] = {6};
const int pattern_7[] = {7, 9, 3, 1};
const int pattern_8[] = {8, 4, 2, 6};
const int pattern_9[] = {9, 1};
int main()
{
	char a[BUFFER_LENGTH];
	long long b;
	while(cin >> a >> b)
	{
		int len_a = strlen(a);
		int last_digit = a[len_a-1] - 0x30;
		int result;
		b--;
		switch(last_digit)
		{
		case 0:
			result = pattern_0[b%pattern_size[0]];
			break;
		case 1:
			result = pattern_1[b%pattern_size[1]];
			break;
		case 2:
			result = pattern_2[b%pattern_size[2]];
			break;
		case 3:
			result = pattern_3[b%pattern_size[3]];
			break;
		case 4:
			result = pattern_4[b%pattern_size[4]];
			break;
		case 5:
			result = pattern_5[b%pattern_size[5]];
			break;
		case 6:
			result = pattern_6[b%pattern_size[6]];
			break;
		case 7:
			result = pattern_7[b%pattern_size[7]];
			break;
		case 8:
			result = pattern_8[b%pattern_size[8]];
			break;
		case 9:
			result = pattern_9[b%pattern_size[9]];
			break;
		default:
			result = -1;
		}
		cout << result << endl;
	}
	return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.