hdu1061(快速幂求模)

时间:2019-02-11
本文章向大家介绍hdu1061(快速幂求模),主要包括hdu1061(快速幂求模)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Given a positive integer N, you should output the most right digit of N^N.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
For each test case, you should output the rightmost digit of N^N.
Sample Input
2
3
4
Sample Output
7
6

Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.

问题简介:输入一个数n,求出n^n的个位数是什么;
问题分析:其实就是快速幂求模,模为10;
程序分析:ab=(a2)(b/2);

#include <iostream>
using namespace std;

int ksm(int a, int x,int m)
{
	int ans=1;
	while (x)
	{
		a = a % m;
		if (x & 1)ans = (ans*a) % m;//奇数乘多一次
		a =(a*a)%m; x /= 2;			//每次乘完都取模,减小计算量
	}
	return ans;
}
int main()
{
	int n,t; 
	cin >> t;
	while (t--) 
	{
		cin >> n;
		cout << ksm(n, n, 10) << endl;
	}

}