Description
输入一个整数X,求一个整数N,使得N!恰好大于X^X。
Format
Input
第一行:一个整数X
Output
第一行:一个整数N
Sample
Input 1
1
Output 1
2
Input 6
1000000000
Output 6
1048199769
写了这道题才知道原来有斯特林公式这种求n!的利器。
这道题的数都太大了,所以我们直接两边取一下对数就好了。
其中斯特林公式中间有相乘的地方,我们都知道对数里面相乘等于拆出来相加,然后就可以求解了。
代码如下:
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;
const double pi = acos(-1);
const double e = 2.718281828459;
const double eps = 1e-10;
int main() {
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
long long x;
scanf("%lld", &x);
double b = x * log10(x);
long long front = 1, back = 1e15;
while(back - front > 1) {
long long mid = (front + back) / 2;
double a = log10(sqrt(2.0 * pi * mid)) + mid * log10(mid / e);
if(a - b > eps) {
back = mid;
} else {
front = mid;
}
}
printf("%lld\n", back);
return 0;
}