模板总结归纳:
//快速幂(取模)
// O(logi)
long long pow_mod(long long a, long long i, long long n)
{
if(i == 0) return 1 % n;
int temp = pow_mod(a, i >> 1, n);
temp = temp * temp %n;
if(i & 1) temp = (long long)temp * a % n;
return temp;
}
实战模板题 :HDU1061 Rightmost Digit
//HDU1061 Rightmost Digit
#include<iostream>
#include<cstdio>
using namespace std;
long long qpow(long long a, long long b)
{
if(b == 0) return 1;
long long temp = qpow(a, b >> 1);
temp = temp * temp % 10;
if(b & 1) temp = (long long)temp * a % 10;
return temp;
}
int main()
{
int n;
cin >> n;
while(n--)
{
long long a;
cin >> a;
cout << qpow(a, a) << endl;
}
return 0;
}