您的位置:首页 > 其它

SPOJ FACVSPOW - Factorial vs Power

2017-02-23 17:15 429 查看


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


对于给定的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 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;
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: