HDOJ--ACMSteps--2.1.8--Leftmost Digit-(取对数,数学)

Problem Description
Given a positive integer N, you should output the leftmost 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 leftmost digit of N^N.
 

Sample Input

2
3
4

 

Sample Output

2
2

Hint

In the first case, 3 * 3 * 3 = 27, so the leftmost digit is 2.
In the second case, 4 * 4 * 4 * 4 = 256, so the leftmost digit is 2.
 
Author
Ignatius.L

首先,暴力算是一定会超时的,而且这么大的数也存不下
这道题运用了取对数来缩小运算范围,
推导过程如下:
设M=N^N,则log10(M)=Nlog10(N);
所以M=10^(N
log10(N));
继续转化
令N=x10^y 例如:155555555=1.5555555510^8;
M=10(N*log10(x*10y)=10(N*(y+log10x))=10(Ny)log10x
so, 只要求出log10x即可
取整即可

posted @ 2016-10-22 22:08  Pic  阅读(174)  评论(0编辑  收藏  举报