2009年3月13日 星期五

Q10515: Power et al.

// Run time: 0.100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int N = 102;
int arr[10] = {1, 1, 4, 4, 2, 1, 1, 4, 4, 2};

int calculate(int m, int n);

int main()
{
char s[N+1], t[N+1];
int len1, len2;
int m, n;
while (scanf("%s%s", s, t))
{
m = atoi(s);
n = atoi(t);

if (!m && !n) break;

if (!m)
{
printf("0\n");
continue;
}

if (!n)
{
printf("1\n");
continue;
}

len1 = strlen(s);
len2 = strlen(t);

m = s[len1-1] - '0'; // digit in ones
n = (t[len2-2]-'0')*10 + t[len2-1]-'0'; // digits in ones and tens


printf("%d\n", calculate(m, n));
}

return 0;
}

int calculate(int m, int n)
{
int i;
int prod = 1;

n = n%arr[m] ? n%arr[m] : arr[m];

for ( i = 0; i < n; i++)
prod = prod*m;

return prod%10;
}

沒有留言: