51nod 1004 n^n的末位数字 快速幂

给出一个整数N,输出N^N(N的N次方)的十进制表示的末位数字。

快速幂淦爆一切!!!!

大大后天会写个快速幂算法的讲解 XD

写了 在下面

https://blog.csdn.net/weixin_41544329/article/details/84646790

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;

ll quickmod(ll n)
{
	ll base=n,ans=1;
	while(n)
	{
		if(n&1)
		ans=(ans*base)%10;
		base=(base*base)%10; 
		n>>=1;
	}
	return ans%10;
}

int main()
{
	ll n;
	scanf("%lld",&n);
	printf("%lld\n",quickmod(n));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41544329/article/details/84504864