杭电1097 http://acm.hdu.edu.cn/showproblem.php?pid=1097
题目大意:求a的b次方的尾数。
快速幂。中间取模即可。
AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stdlib.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
int a,b;
int qs(ll a,ll b) {
ll r=1;
while(b) {
if(b & 1) r = r * a % 10;
a = a * a % 10;
b>>=1;
}
return r;
}
int main() {
while(cin >> a >> b) {
cout<< qs(a,b) << endl;
}
return 0;
}