您的位置:首页 > 其它

FACVSPOW - Factorial vs Power 数学方法 + 二分

2017-02-20 14:08 351 查看
http://www.spoj.com/problems/FACVSPOW/

求解n! > a^n最小的整数n

对于有n!和a^n的东西,一般是取ln

然后就是求解

(ln(1) + ln(2) + .... + ln(n)) / n > ln(a)的最小整数n

发现左边的函数单调,所以可以预处理出来,右边最大值是ln(1e6)

所以预处理5e6个。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 5e6 + 20;
double f[maxn];
void init() {
for (int i = 1; i <= maxn - 20; ++i) {
f[i] = (f[i - 1] * (i - 1) + log(i * 1.0)) / (i * 1.0);
}
//    for (int i = 1; i <= 100; ++i) {
//        cout << f[i] << endl;
//    }
}
int a;
double up;
const double eps = 1e-12;
bool bigger(double a, double b) {
return a - b > eps;
}
void work() {
scanf("%d", &a);
up = log(a * 1.0);
int be = 1, en = maxn - 20;
while (be <= en) {
int mid = (be + en) >> 1;
if (bigger(f[mid], up)) {
en = mid - 1;
} else be = mid + 1;
}
printf("%d\n", be);
}

int main() {
#ifdef local
freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
init();
int t;
scanf("%d", &t);
while (t--) work();
return 0;
}


View Code

据说

ln(n!) ≈ (n * ln(n)) - n + (1 / 2 * ln(2 * PI * n));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: