码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1018 -- Big Number (Stirling公式求n!的位数)

时间:2018-02-05 23:31:48      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:clu   tle   pid   its   mis   dig   main   int   content   

题目链接: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 }
View Code

 

HDU 1018 -- Big Number (Stirling公式求n!的位数)

标签:clu   tle   pid   its   mis   dig   main   int   content   

原文地址:https://www.cnblogs.com/kangkang-/p/8419369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!