一,思路
快速幂算法的核心思想就是每一步都把指数分成两半,而相应的底数做平方运算。
二.代码解法
C++
#include<bits/stdc++.h>
using namespace std;
int n,a[1999999];
int fun(int a, int b)
{
int t, x;
x =a %1000;
t = 1;
while( b )
{
if( b % 2 == 1 )
{
t *= x;
t %= 1000;
}
x *= x;
x %= 1000;
b /= 2;
}
return t;
}
int main()
{
int n;
int h;
cin>>h;
while(h--)
{
cin>>n;
cout<<fun(n,n)%10<<endl;
}
return 0;
}