摘自百度:
斯特林公式(Stirling's approximation)是一条用来取n的阶乘的近似值的数学公式。
形 式 :
或更精确的
或
1. HDU1018:http://acm.hdu.edu.cn/showproblem.php?pid=1018
题意:求n!的位数。
题解:
暴力统计log10(1)+……log10(n),然后向上取整即可。
O(1)询问的话,就用斯特林公式,对那个式子取log10。得到:log10(n!) = log10(sqrt(2*PI*n)) + nlog10(n/e) ,其中 PI 为圆周率acos(-1.0),e 为自然底数exp(1.0)。
代码:
#include<bits/stdc++.h>
using namespace std;
const double e=exp(1.0);
const double PI=acos(-1.0);
int main()
{
int T,n;
double ans;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
ans=log10(sqrt(2.0*PI*n))+(double)n*log10((double)n/e);
printf("%d\n",(int)ceil(ans));
}
return 0;
}