HDU leftmost Digit

Source: Internet
Author: User

Thought: At the beginning, I found it very tangled !!

Later, I saw a person's analysis, which was converted in this way.

M = n ^ N; obtain the logarithm of the two sides. log10 (m) = N * log10 (n); then, M = 10 ^ (N * log10 (n ));

Then, for the integer power of 10, the first digit is 1. Therefore, the first digit depends on the decimal part of N * log10 (n ).

In short, log is very powerful. In order to calculate the number of digits of a number, the big integer is converted into an integer within the range, which is an exponential problem.

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 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. Code:
 
# Include <iostream> # include <cmath> using namespace STD; int main () {_ int64 I, T, A, sum; double S, X, N; cin> T; while (t --) {CIN> N; S = N * log10 (n); A = (_ int64) s; X = S-; sum = (_ int64) Pow (10.0, x); cout <sum <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.