FACVSPOW - 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 than g(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
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define LONG long long
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
bool check(LONG n ,double a )
{
double x = (double )n;
double sum = x * log(x) - x + 0.5*(log(PI * 2 * x));
if(sum > x * log(a))
return 1;
else return 0;
}
int main()
{
int T;
cin>>T;
while(T--)
{
double a ;
cin>>a ;
LONG l = 1 , r=10*a;
LONG mid ;
while(l < r)
{
mid = (l + r ) / 2;
if(check(mid,a))
{
r = mid ;
}
else
l = mid + 1;
// cout<<mid<<endl;
}
cout<<l<<endl;
}
}