SPOJ FACVSPOW

来源:互联网 发布:30多了能做网络主播吗? 编辑:程序博客网 时间:2024/05/16 17:49

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 kf(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:3234Output:479
对于给定的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<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 longconst 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-10bool 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;    }}


1 0