Factorial vs Power
Consider two integer sequences f(n) = n! and g(n) = an, where n 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 integer k, f(n) is greater thang(n) for all n >= k. You are to find the least positive value of n for which f(n) > g(n), for a given positive integer a > 1.
Input
The first line of the input contains number t – the amount of tests. Then t test descriptions follow. Each test consist of a single number a.
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: 3 2 3 4 Output: 4 7 9
题意:输入a,找到满足n!>a^n 最小的n。
题解:
n*(n-1)*...*2*1>a*a*a...*a
ln(n)+ln(n-1)+...+ln(2)+ln(1)>ln(a)+ln(a)...+ln(a)
ln(a)<(ln(n)+ln(n-1)+...+ln(1))/n
预处理完二分查找
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
double pre[3000005];
int main(){
int n,t,i;
double now=0;
for(i=1;i<=3000000;i++){
now+=log(i);
pre[i]=now/i;
}
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int l=1,r=3000000;
double d=log(n);
while(l+1<r){
int mid=l+r>>1;
if(d<pre[mid])r=mid;
else l=mid;
}
if(d<pre[l])printf("%d\n",l);
else printf("%d\n",r);
}
return 0;
}