Big number
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 25183 accepted submission (s): 11426
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 ≤0 ≤ 107 on each line.
Outputthe output contains the number of digits in the factorial of the integers appearing in the input.
Sample input21020
Sample output719 problem-solving analysis: (turn) N for this question! The number of digits of the result, because the data range is 1 ~ 10000000, the conventional solution is definitely not good. If the array is used for the multiplication of large numbers, the array will not be saved, because 10000000! It's too big. So what should we do?
Before that, we must know that the number of digits of any positive integer a is equal to (INT) log10 (A) + 1. Why? Let's deduce the following: for any given positive integer a, suppose 10 ^ (x-1) <= A <10 ^ X, then the number of digits of a is obviously X, because log10 (10 ^ (x-1) <= log10 (a) <(log10 (10 ^ X) is the X-1 <= log10 (a) <X then (INT) log10 (A) = X-1, that is, (INT) log10 (A) + 1 = X that is, the number of digits of a is (INT) log10 () + 1 we know that the number of digits of a positive integer is equal to (INT) log10 (A) + 1. Now we want to calculate the number of digits of the factorial of N: Suppose A = n! = 1*2*3 *...... * n, so what we need is (INT) log10 (A) + 1, and: log10 (A) = log10 (1*2*3 *...... n) (according to log10 (A * B) = log10 (A) + log10 (B) = log10 (1) + log10 (2) + log10 (3) + ...... + log10 (n) Now we have finally found a solution and solved the problem. We break down the number of digits of the factorial of N into the sum of the logarithm of the number of N to 10, and any number is in the normal numeric range. To sum up, the number of factorial digits of N is equal to (INT) (log10 (1) + log10 (2) + log10 (3) + ...... + log10 (N) + 1
You can write the problem-solving code:
1 #include <math.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 int main(){ 6 int T, m; 7 double ans; 8 scanf ("%d", &T); 9 while(T --){10 ans = 0;11 scanf("%d", &m);12 for (int i = 1; i <= m; i ++)13 ans += log10(i);14 printf ("%d\n", (int)ans + 1);15 }16 return 0;17 }
View code