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 ≤ 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
2
10
20
Sample Output
7
19
应用Stirling公式 利用Stirling公式求解n!的位数:
lgn! =0.5*lg (2*π*n)+n*lg(n/e)
所以n!的位数为: res = lgn!+1
#include <bits/stdc++.h>
int main()
{
int n,x;
while(~scanf("%d",&n))
{
while(n--)
{
scanf("%d",&x);
double res = 0;
for(int i = 1;i <= x;i++)
{
res += log10(i * 1.0);
}
printf("%d\n",(int)res+1);
}
}
return 0;
}
#include<bits/stdc++.h>
const double PI = acos(-1);
const double e = 2.718281828459;
int main()
{
int n,x;
while(~scanf("%d",&n))
{
while(n--)
{
scanf("%d",&x);
double res = 0;
res = 0.5*log10(2*PI*x)+x*log10(x/e);
printf("%d\n",(int)res+1);
}
}
}
本文介绍了一种计算大整数阶乘位数的方法,适用于加密等应用场景。通过两种方式实现:直接计算和使用Stirling公式进行估算。
403

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



