Leftmost digit (solution Report)

Source: Internet
Author: User
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 Input
234
 
Sample output
22
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.

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.