题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018
Big Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40551 Accepted Submission(s): 19817
Problem Description
In many 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 ≤ n ≤ 107 on each line.
Output
The output contains the number of digits in the factorial of the integers appearing in the input.
Sample Input
2
10
20
Sample Output
7
19
题意:求n!有多少位。
解析:Stirling公式:n! ≈ √(2πn) * n^n * e^(-n);
故log10(n!) = log(n!) / log(10) = ( n*log(n) - n + 0.5*log(2*π*n))/log(10);
n!的位数=log10(n!) + 1, 故加上ceil();
若要求其他进制的位数而非十进制,加个换底公式即可,即把最后的log(10)改成log(进制数);
注意n为0和1时的特判;
0!= 1;
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 int Stirling(int n) { 5 return ceil((n * log(double(n)) - n + 0.5 * log(2.0 * n * acos(-1.0))) / log(10.0)); 6 } 7 8 int main() { 9 int t, n; 10 cin >> t; 11 while (t--) { 12 cin >> n; 13 cout << (n <= 1 ? 1 : Stirling(n)) << endl; 14 } 15 return 0; 16 }