Problem: Find the leftmost number of n^n and the rightmost one, N (1<=n<=1,000,000,000).
Find the right-hand side:
Analysis: Actually find the left and right side of a number is quite simple, fast power each time only take the result of the last one to participate in the next operation, take the final result of the last
Find the leftmost one:
The n^n can be represented by scientific notation, and the integer part of the valid number represented by the scientific notation is the answer
Enter an N,
Use scientific notation to denote n^n = a * 10^n, where the integral part of a has only one
Both sides take the logarithm at the same time, get N*lg (N) = LG (a) + N
Because 1 < A < 10, so 0 < LG (a) < 1, and n is an integer, that is, LG (a) is a fractional part of N*LG (n), and n is an integer portion of N*LG (n),
So 10^ (N*LG (n)-(int) N*LG (n)) is the result.
Note: The result is larger when you can use __int64 or long long (same)
Find the right number title: http://acm.hdu.edu.cn/showproblem.php?pid=1061
Find the leftmost number topic: http://acm.hdu.edu.cn/showproblem.php?pid=1060
My Code:
Most right:
#include <stdio.h>#include<math.h>#defineLL __int64ll Qfact (ll N) {ll res=1, pow =N; while(n) {if(N &1) {res*=POW; Res%=Ten; } POW*=POW; Pow%=Ten; N/=2; } returnRes;}intMain () {intT; scanf ("%d", &t); while(t--) {LL n, ans; scanf ("%i64d", &N); Ans=qfact (n); printf ("%i64d\n", ans%Ten); } return 0;}
Leftmost: (Scientific counting method)
#include <stdio.h>#include<math.h>#defineLL __int64intMain () {intT; scanf ("%d", &t); while(t--) {LL n; scanf ("%i64d", &N); DoubleAns = N*LOG10 (n1.0); Ans= ans-(LL) ans; Ans= POW (10.0, ans); printf ("%i64d\n", (LL) ans); } return 0;}
Find the leftmost single number and the last side of the n^n