[牛客OI测试赛2]F假的数学游戏(斯特灵公式)

时间:2023-03-08 20:41:23

题意

输入一个整数X,求一个整数N,使得N!恰好大于$X^X$。

Sol

考试的时候只会$O(n)$求$N!$的前缀和啊。

不过最后的结论挺好玩的

$n! \approx \sqrt{2 \pi n} (\frac{n}{e})^n$

然后就可以$O(1)$算啦

/*
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
#include<map>
#include<cmath>
#define Pair pair<int, int>
#define fi first
#define se second
#define MP(x, y) make_pair(x, y)
#define LL long long
const LL MAXN = 1e8 + , mod = , inv = ;
using namespace std;
inline LL read() {
char c = getchar(); LL x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
const double pi = acos(-), e = exp(1.0);
LL N, X;
double up;
bool check(double n) {
return 0.5 * log( * pi * n) + n * log(n / e) >= up;
}
int main() {
X = read();
/*if(X == 7) {printf("10"); return 0;}
if(X == 77) {printf("94"); return 0;}
if(X == 777) {printf("892"); return 0;}
if(X == 7777) {printf("8640"); return 0;}
if(X == 77777) {printf("84657"); return 0;}
if(X == 777777) {printf("834966"); return 0;}
if(X == 7777777) {printf("8267019"); return 0;}
if(X == 77777777) {printf("82052137"); return 0;}
if(X == 777777777) {printf("815725636"); return 0;}
if(X == 7777777777ll) {printf("8092563686"); return 0;}*/
up = X * log(X);
// for(LL i = 1; i <= 1e8; i++) lg[i] = log(i), s[i] = s[i - 1] + lg[i];
//cout << lg[10];
int times = ;
double l = , r = 1e13, ans;
while(times--) {
LL mid = (l + r) / ;
if(check(mid)) ans = mid, r = mid;
else l = mid;
}
cout << (long long)ans;
return ;
}
/*
2
4 6
4 6
*/