View Code of Problem 3696

#include<stdio.h>
#include<string.h>
#include<math.h>
//每次末尾数字相乘即可 ,会超时QAQ 
//尝试二分法,求n的n次末尾,必先求n的n/2的末尾 
int mowei(int n,int m)
{
	if(m==1)
	{
		return n%10;
	}
	else 
	{
		if(m%2==0)
		return ((mowei(n,m/2)%10)*(mowei(n,m/2)%10))%10; //m为偶数 
		else return ((mowei(n,m/2)%10)*(mowei(n,m/2)%10)*n)%10;//m为奇数 
	}
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int m=mowei(n,n); 
		printf("%d\n",m);
	}
}

Double click to view unformatted code.


Back to problem 3696