Big number
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 27546 Accepted Submission (s): 12525
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 Input2 10 20
Sample Output7 19 is easy to get answers with simulations, but 10 of 7!!!!!! No doubt timeout over memory. Here a Stirling formula is used, and the Sterling formula (Stirling's approximation) is a mathematical formula for the approximate value of the factorial of N. In general, when N is very large, the calculation of n factorial is very large, so the sterling formula is very useful, and even when N is very small, the value of the sterling formula is very accurate. Stirling Formula:But only with this formula is not enough, because the factorial of N to more than 10 of the super int range, this question to calculate n! factorial of the number of bits, for a number A, if there is 10^ (x-1) <a<10^x, then A is the X-bit integer, so the number of bits of n! to the formula on both sides take 10 as the base of the logarithm, Figure out is a decimal, converted to int after adding 1.x=int (lgn!) =int ((LG2*PI+LGN)/2+n* (Lgn-lge)) +1. Pay attention to the situation of n=1.
1#include <iostream>2#include <cmath>3 using namespacestd;4 5 intMain ()6 {7 intn,m;8 Doubles;9Cin>>N;Ten while(n--) One { As=0.0; -Cin>>m; - if(m==1) thes=0; - Else -S= (LOG10 (2*3.141592654)*0.5+LOG10 (M) *0.5+m* (log10 (m)-log10 (2.718281828459))); -cout <<int(s+1) <<Endl; + } - return 0; +}
View Code
HDU 1018 Big Number