NEFU 66 leftmost number

Source: Internet
Author: User
Leftmost number
Time limit 1000 ms Memory limit 65536 K
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
234
Sample_output
22

The question is very understandable, that is, the number on the leftmost side of N ^ N, the range of N is so large, it is obvious that you don't need to think about it directly. What should we do? Of course, the formula is used:

10 ^ (N * lg (N)-[N * lg (n)]) = POW (10, N * log10 (N)-(INT) (N * lg (N )))

I want to explain how this formula comes from:

Set n ^ n = A0 * 10 ^ m + A1 * 10 ^ m-1) +...

A0, A1. .. is the coefficient of the corresponding digit. m is the number of digits, for example, 4 ^ 4 = 256, a0 = 2, a1 = 5, a2 = 6, M = 3;

Obviously, a0 is the leftmost number, that is, what we want. Then we will ask A0;

A0 * 10 ^ m <= n ^ n <(a + 1) * 10 ^ m

The logarithm of the two keys is as follows:

M + lga0 <= nlgn <m + lg (A0 + 1 ).................................... (#)

That is, lga0 <= nlgn-M <lg (A0 + 1)

Therefore, a0 <= 10 ^ (nlgn-m) <A0 + 1, a0 is an integer, A0 + 1 is also an integer, the two are adjacent, and 10 ^ (nlgn-m) if the value of A0 + 1 is not obtained, the 10 ^ (nlgn-m) is rounded down to A0, so a0 = [10 ^ (nlgn-m)];

At this step, M is unknown, and m is to be obtained. You need to analyze A0 first:

Because 1 <= A0 <= 9, 0 <= lg (A0) <1, while 0 <lg (A0 + 1) <= 1;

Let's look at the (#) formula:

M + lga0 <= nlgn <m + lg (A0 + 1), respectively get the integer: M = [nlgn], so m is obtained.

So a0 = [10 ^ (N * lgn-m)] = [10 ^ (N * lgn-[N * lg (n)];

Code:

#include <stdio.h>#include <string.h>#include <math.h>int main(){int i,t,ans;double n;scanf("%d",&t);while(t--){scanf("%lf",&n);ans=(int)pow(10,(n*log10(n)-(int)(n*log10(n))));printf("%d\n",ans);}return 0;}


Now let's expand it, that is, ask n! The formula for the number of digits:

LG (1) + lg (2) + lg (3) + ......... LG (n) + 1

After using the real sum, convert int to + 1. You can do the NEFU 65th question.

NEFU 66 leftmost number

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.