[笔记] 斯特林公式
[笔记] 斯特林公式
先看一道题
题目大意:求一个\(N\)的阶乘恰好大于\(x^x\)
用斯特林公式算出位数,然后二分查找,一定注意精度
Detail
\[\displaystyle{\lim\limits_{n \rightarrow \infty}\frac{n!}{\sqrt{2 \pi n}\left(\frac{e}{n}\right)^n}=1}
\]
\[\displaystyle{\frac{\lg{2 \pi n}}{2}+n\lg{\frac{e}{n}}}
\]
Code
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const double Pi = acos(-1.0);
const double e = exp(1.0);
typedef long long ll;
long long x;
int main(){
cin >> x;
ll l = 1, r = 100000000000000L, mid;//字面值太大要加个L保险
while(l < r){
mid = l + ((r - l) >> 1);
if(double(double(log10(sqrt(2 * Pi * mid))) + double(mid * double(log10(mid / e)))) < x*double(log10(x)))
l = mid + 1;
else
r = mid;
}
printf("%lld\n", l);
return 0;
}