Big Numberhttp: // poj.org/problem? Id = 1423
Time Limit:1000 MS |
|
Memory Limit:65536 K |
Total Submissions:24637 |
|
Accepted:7895 |
Description
In specified 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.
Input
Input 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 <= m <= 10 ^ 7 on each line.
Output
The output contains the number of digits in the factorial of the integers appearing in the input.
Sample Input
21020
Sample Output
719
Source
Dhaka 2002 train of thought: log10 (n !) = Log10 (1*2*3... * N) = log10 (1) + log10 (2) +... + Log10 (n)
Method 1: use space for time and open an array to save the result!
# Include "stdio. h "# include" math. h "# include" string. h" # define N 8000000 double a [N]; void Inti () {int I; a [0] = 0; for (I = 1; I
Method 2: n! = Sqrt (2 * π * n) * (n/e) ^ n) * (1 + 1/(12 * n) + 1/(288 * n) + O (1/n ^ 3) π = acos (-1) e = exp (1) both sides take the logarithm of 10
Ignore log10 (1 + 1/(12 * n) + 1/(288 * n) + O (1/n ^ 3) ≈ log10 (1) = 0 get the formula
Log10 (n !) = Log10 (sqrt (2 * pi * n) + n * log10 (n/e ).
# Include "stdio. h"
# Include "math. h"
# Include "string. h"
# Define N 80000
Int main ()
{
Int t, n, I;
Double s, pi, e;
Pi = acos (-1 );
E = exp (1 );
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & n );
S = log10 (sqrt (2 * pi * n) + n * log10 (n/e );
If (n = 1)
S = 1;
Printf ("% d \ n", (int) ceil (s ));
}
Return 0;
}