Factorial vs Power

来源:互联网 发布:常见文本文件格式知乎 编辑:程序博客网 时间:2024/06/06 09:46

Consider two integer sequences f(n) = n! and g(n) = an, wheren is a positive integer. For any integer a > 1 the second sequence is greater than the first for a finite number of values. But starting from some integerk, f(n) is greater than g(n) for alln >= k. You are to find the least positive value of n for whichf(n) > g(n), for a given positive integer a > 1.

Input

The first line of the input contains number t – the amount of tests. Thent test descriptions follow. Each test consist of a single numbera.

Constraints

1 <= t <= 100000
2 <= a <= 106

Output

For each test print the least positive value of n for which f(n) > g(n).

Example

Input:3234Output:47

9

对于给定的a,求满足的 n! > an  最小的n
取对数,然后发现[ln(1)+ln(2)+ln(3)+ln(4)+ ……+ln(n)]/n 和n是线性关系的,所以可用二分来求满足>n*ln(a)的最小n
利用斯特林公式
ln(n!) = n * ln(n) - n + 0.5*(ln(PI * 2 * n))

#include<cstdio>#include<cmath>#include<algorithm>#define PI acos(-1)#define ll long long using namespace std;bool check(ll n, int a) {double s = (double)n;double sum = s * log(s) - s + 0.5*(log(PI * 2 * s));if (sum > s*log(a)) return true;return false;}int main() {int T;scanf("%d", &T);double a;while (T--) {scanf("%lf", &a);ll l = 0, r = 99999999;ll mid;while (l < r) {mid = (l + r) / 2;if (check(mid, a)) {r = mid;}else l = mid+1;}printf("%lld\n", l);}}


原创粉丝点击