Big Number |
| Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
| Total Submission(s): 586 Accepted Submission(s): 398 |
|
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 |
思路:斯特林公式:lnN!=NlnN-N+0.5*ln(2*N*pi),而N!的位数等于log(N!)+1.
AC代码:
#include<iostream>
#include<cmath>
using namespace std;
const double PI=acos(-1.0);
int f(int n)
{
double res;
res=(n*log(n*1.0)-n+0.5*log(2*n*PI))/log(10.0);
res=floor(res)+1;
return res;
}
int main()
{
int n,t;
cin>>t;
while(t--)
{
cin>>n;
cout<<f(n)<<endl;
}
return 0;
}
本文介绍了一种利用斯特林公式计算大整数阶乘位数的方法,并提供了具体的AC代码实现。斯特林公式可以帮助我们快速估算大整数阶乘的对数值,进而确定其位数。
2万+

被折叠的 条评论
为什么被折叠?



