#include<iostream> #include<cstdio> using namespace std; long long int quickpow(long long int m,int n,int k) { long long int ans=1; while(n>0) { if(n&1) ans=(ans*m)%k; n=n>>1; m=(m*m)%k; } return ans; } int main() { long long int a; int b; while(cin>>a>>b) { cout<<quickpow(a,b,10)<<endl; } return 0; }
亲测用快速幂模板~如果ans和m不用longlong会爆int~,当然可以不这样~
代码如下~
#include<iostream> #include<cstdio> using namespace std; int quickpow(int m,int n,int k) { m=m%k; int ans=1; while(n>0) { if(n&1) ans=(ans*m)%k; n=n>>1; m=(m*m)%k; } return ans; } int main() { int a; int b; while(cin>>a>>b) { cout<<quickpow(a,b,10)<<endl; } return 0; }