Finding the exponent of any number can be very troublesome as it grows exponentially. But in this problem you will have to do a very simple task. Given two non-negative numbers m and n, you will have to find the last digit of mn in decimal number system.

Input

The input file contains less than 100000 lines. Each line contains two integers m and n (Less than 10^101. Input is terminated by a line containing two zeroes. This line should not be processed.

Output

For each set of input you must produce one line of output which contains a single digit. This digit is the last digit of m^n.

Sample Input

2 2

2 5

0 0

Sample Output

4

2

计算n^m 的最后一位。

本题的关键是n和m都非常大。

对于n，只需要取最后一位进行计算就可以了。

对于m，处理起来就比较麻烦，需要掌握其规律。1位数字d的n次方，即d^n，会每4次方出现循环，所以只需要考虑n的最低2位（百位以上是4的倍数），再做个模除4再行计算即可。

AC的C++语言程序如下：
/* UVA10515 Powers Et Al. */

#include <bits/stdc++.h>

using namespace std;

const int MOD = 4;

int getb(string n)
{
if (n.size() == 1)
return n[0] - '0';
int b =n[n.size() - 1] - '0';
b = b + (n[n.size() - 2] - '0') * 10;
b %= 4;
if (b == 0)
b=4;
return b;
}

int main()
{
string m, n;
while(cin >> m >> n && (m != "0" || n != "0")) {
int a = m[m.length() - 1] - '0';
int b = getb(n);

int ans = a;
for(int i = 1; i < b; i++)
ans *= a;

printf("%d\n", ans % 10);
}

return 0;
}



• 评论

• 上一篇
• 下一篇