HDU 1018-big Number (math)

Source: Internet
Author: User
Tags integer numbers

Big numberTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 26383 Accepted Submission (s): 12006


Problem DescriptionIn Many applications very large integers numbers are required. Some of these applications is using keys for secure transmission of data, encryption, etc. In this problem you is given a number, you has to determine the number of digits in the factorial of the number.

Inputinput consists of several lines of integer numbers. The first line contains a integer n, which is the number of cases to being tested, followed by n lines, one integer 1≤n≤ 107 on all line.

Outputthe output contains the number of digits in the factorial of the integers appearing in the input.

Sample Input
21020

Sample Output
719 the number of digits for the factorial of the large number. Written in Java violence. Then I think I really brain remnant 10^7 not T is strange. See the formula to think that math is really powerful. Derivation process:
 Before this, we need to know a knowledge, arbitrarily a positive integer A is equal to (int) log10 (a) + 1. Why is it?  Here's a deduction for you: For random a given positive integer a. If 10^ (x-1) <=a<10^x, then it is clear that the number of digits of a is X-bit, and because Log10 (10^ (x-1)) <=log10 (a) < (LOG10 (10^x)) is X-1<=log10 (a) <x ( int) log10 (a) =x-1, i.e. (int) log10 (a) +1=x that is (int) log10 (a) +1 we know the number of bits of a positive integer A is equal to (int) log10 (a) + 1, and now the number of bits of the factorial of N: if a=n!=1*2         *3*......*n, then we are asking for (int) log10 (a) +1, and: log10 (a) =log10 (1*2*3*......N) (based on log10 (a*b) = log10 (a) + log10 (b)) =LOG10 (1) +log10 (2) +log10 (3) +......+log10 (n) Now we finally find a way to overcome the problem. We will find the number of the factorial of n decomposition into the number of N to 10 to take the logarithm of the same, and for a random number, are in the normal range of numbers and so on. Sum up: The number of factorial bits of n equals (int) (LOG10 (1) +log10 (2) +log10 (3) +......+log10 (n)) + 1 
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib > #include <algorithm> #define LL long longusing namespace Std;int main () {    int t,n;    scanf ("%d", &t);    while (t--) {scanf ("%d", &n);    Double ans=0;    for (int i=2;i<=n;i++) ans+=log10 (i);p rintf ("%d\n", 1+ (int) ans);    return 0;}



Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

HDU 1018-big Number (math)

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.