Leftmost Digit |
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others) |
Total submission (s): 364 accepted submission (s): 198 |
|
Problem descriptiongiven a positive integer N, you should output the leftmost digit of N ^ n. |
Inputthe 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 ). |
Outputfor each test case, You shoshould output the leftmost digit of N ^ N. |
Sample Input234 |
Sample output22 HintIn 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. |
The question is to enter N and find the highest digit of N ^ n. 1 <= n <= 1,000,000,000
It is estimated that we have no idea about the N range. The number N is indeed too large. If you want to calculate the result, it will time out even if it does not overflow.
I have been struggling with this question for a long time. At the prompt of the students, the AC is displayed.
The question is converted in this way.
First, use scientific notation to represent n ^ n = A * 10 ^ X; for example, n = 3; 3 ^ 3 = 2.7*10 ^ 1;
The rightmost number we require is (INT) A, that is, the integer part of;
OK, and then obtain the base-10 logarithm lg (N ^ n) = lg (A * 10 ^ X) on both sides );
Simplify N * lg (n) = lg (A) + X;
Continue N * lg (N)-x = lg ()
A = 10 ^ (N * lg (N)-x );
Now only X is unknown. If n is used to represent X, this problem is solved.
Because X is the number of digits n ^ n. For example, N ^ n = 1200 => X = 3; X is actually an integer down from lg (N ^ N), which is expressed as [lg (N ^ n)].
OK a = 10 ^ (N * lg (N)-[lg (N ^ n)]); then (INT) A is the answer.
Code:
#include<iostream>#include<math.h>int main(){int n,m;std::cin>>n;while(n--){std::cin>>m;long double t = m*log10(m*1.0);t -= (__int64)t;__int64 ans = pow((long double)10, t);std::cout<<ans<<std::endl;}return 0;}