HDU 1018 Big Number

Source: Internet
Author: User

This question requires the number of bits of the factorial of N, if n is larger, the factorial of n is bound to be a
A large number, the title said 1<=n<10000000, when n=10000000 can say n
The factorial would be a very huge number, for dealing with large numbers of problems, I
We usually use a string, which is when n takes the maximum value, it is 10 million digital phase
Multiply the product, too big, even if saved in the string is a little difficult, and
Multiplying by 10 million numbers is a multiplication involving large numbers, and the multiplication of large numbers is a comparison
Time-consuming, even if the results are calculated in general will time out. It makes us want to abandon
Such a direct approach.

Think again, this question is the number of bits that require the factorial of n, and the factorial of n is the number of n
Product, then it would be nice if we could break the problem down.

Before that, we have to know a bit of knowledge that any number of positive integers a
equals (int) log10 (a) + 1; Why? Here is a deduction for you:

For any given positive integer a,
Assuming 10^ (x-1) <=a<10^x, then it is obvious that the number of a is X-bit,
And because
LOG10 (10^ (x-1)) <=log10 (a) < (LOG10 (10^x))
That is X-1<=log10 (a) <x
Then (int) log10 (a) =x-1,
i.e. (int) log10 (a) +1=x
That is, the number of bits of a is (int) log10 (a) +1

We know that a positive integer A has a number of digits equal to (int) log10 (a) + 1,
Now to find the number of bits of the factorial of N:
Assuming a=n!=1*2*3*......*n, then what we're asking for IS
(int) log10 (A) +1, while:
LOG10 (A)
=LOG10 (1*2*3*......N) (according to log10 (a*b) = log10 (a) + log10 (b) Yes)
=LOG10 (1) +log10 (2) +log10 (3) +......+log10 (n)
Now that we finally find the solution, the problem is solved, and we will seek the bits of N's factorial
The number is decomposed to the number of N to 10 logarithm, and for any number of them,
are in the normal range of numbers.

Sum up: The number of digits of the factorial of n equals
(int) (log10 (1) +log10 (2) +log10 (3) +......+log10 (n)) + 1

1#include <iostream>2#include <cmath>3 using namespacestd;4 intMain ()5 {6     intN,t;cin >>T;7      while(t--){8CIN >>N;9         Doubleans=0;Ten          for(intI=1; i<=n;++i) Oneans+=log10 (i); Acout <<int(ANS) +1<<Endl; -     } -}

HDU 1018 Big 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.