//超时代码,需要o(logn)
#include <stdio.h>
int main()
{
int m,n,i,ans=1;
while (scanf("%d%d",&m,&n)!=EOF)
{
for (i=1;i<=n;i++)
{
ans*=m;
ans%=10;
}
printf("%d\n",ans);
}
}
#include<stdio.h>
long pow_mod(int m, int n, int p)
{
//if(!n) return 1;
long temp;
long ans;
if(0==n)
return 1;
temp = pow_mod(m, n/2, p);
ans = temp * temp % p;
if(n&1) ans = ans * m % p;
return ans;
}
int main()
{
long m, n;
while(~scanf("%ld%ld", &m, &n))
printf("%ld\n", pow_mod(m, n, 10));
return 0;
}
我发现全按long可以
m n不安long会停止
//也对
int pow_mod(int x,int y,int p)
{
long long t=x;
long long ans=1;
while(y)
{
if(y&1)
ans=t*ans%p;
t=t*t%p;
y=y>>1;
}
return (int)ans;
}*/
int main()
{
int m,n;
while(~scanf("%d%d",&m,&n))
printf("%d\n",pow_mod(m,n,10));
return 0;
}
//运用abmodp=(amodp)*(bmodp)modp
自己用个变量打印一下递归调用次数你就知道了