HDU-1018 big number Sterling Formula

Source: Internet
Author: User
Tags integer numbers

Big number

Time Limit: 20000/10000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 10040 accepted submission (s): 4476

Problem descriptionin response applications very large integers numbers are required. some of these applications are using keys for secure transmission of data, encryption, etc. in this problem you are given a number, you have to determine the number of digits in the factorial of the number.

Inputinput consists of several lines of integer numbers. The first line contains an integer N, which is the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 107 On each line.

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

Sample Input

 
21020

Sample output

 
719

Sourceasia 2002, Dhaka (Bengal)

Recommendjgshining

When I first saw the question, I thought of a large number. However, looking at the question, the data given may reach the level of 10 ^ 7. For such a large number, ask for a factorial! Oh, my God! You know, the 10000000 factorial has more than 60 million digits.

Fortunately, we have a formula,Log10 (N !) = (0.5 * log (2 * pI * n) + N * log (N)-N)/log (10). Here we use log10 (n) = ln (N) /ln (10 );

The formula is named the link http://zh.wikipedia.org/wiki/%E6%96%AF%E7%89%B9%E6%9E%97%E5%85%AC%E5%BC%8F of the sterling formula Wikipedia


 
# Include <stdio. h> # include <stdlib. h> # include <math. h> # include <string. h> # include <time. h> # define PI 3.1415926int main () {int t; scanf ("% d", & T); While (t --) {int digits, N; scanf ("% d", & N); If (n = 0) // No .. {printf ("1 \ n"); continue;} digits = (INT) (0.5 * log (2 * pI * n) + N * log (N)-N) /log (10); printf ("% d \ n", digits + 1); // Add 1. } 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.