找最后一位的周期即可。
AC代码如下:
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int result[10][4] = { {}, {}, { 6, 2, 4, 8 }, { 1, 3, 9, 7 }, { 6, 4 }, {}, {}, { 1, 7, 9, 3 }, { 6, 8, 4, 2 }, { 1, 9 } };
int a, b, temp;
while( scanf( "%d%d", &a, &b ) != EOF ){
temp = a % 10;
if( temp == 0 || temp == 1 || temp == 5 || temp == 6 ){
cout << temp << endl;
}else if( temp == 4 || temp == 9 ){
cout << result[temp][b%2] << endl;;
}else{
cout << result[temp][b%4] << endl;
}
}
return 0;
}