描述
给定a和b,输出a^b的最后一个数字。
输入
输入数据有多组,每组数据占一行,每行为a和b的值(0<a,b<=2^30)
输出
对每组输入数据,输出a^b的最后一位数字,每组数据占一行。
样例输入
2 2
3 4
3 4
样例输出
4
1
1
因为数据量很大,所以我用到了快速冥,下面是代码:
#include<iostream>
using namespace std;
int mod(int a,int b,int c)
{
int ans=1,temp=a%c;
while(b)
{
if(b%2)
ans=ans*temp%c;
temp=temp*temp%c;
b/=2;
}
return ans;
}
int main()
{
int n,m;
while(cin>>n>>m)
{
cout<<mod(n,m,10)<<endl;
}
return 0;
}