n^nの末尾の数字(高速べき乗型取り)


整数Nを与え、N^N(NのN次数)の10進数で表される末尾の数字を出力する.
Input
    
個数N(1<=N<=10^9)
Output
 
N^Nの末尾数字を出力
 
Sample Input
13

Sample Output
3
 
  
#include
#include
#include
#include 
using namespace std;

int power_mod(int a,int b,int c)   //      
{
	int ans = 1;
	a = a % c;
	while(b!=0)
	{
		if(b % 2 == 1) ans = (ans * a) % c;
		b = b / 2;
		a = (a * a) % c;
	}
	return ans;
}
int main()
{
	int n;
	scanf("%d",&n);
	printf("%d
",power_mod(n,n,10)); return 0; }