SPOJ FACVSPOW - Factorial vs Power

本文探讨了两个整数序列f(n)=n!与g(n)=a^n的关系,提出了一种利用斯特林公式和二分查找的方法来找到使得n! > a^n成立的最小正整数n。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:
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))

 n! \approx \sqrt{2\pi n}\, \left(\frac{n}{e}\right)^{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;
    }
}

 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值