SPOJ-FACVSPOW - Factorial vs Power(数论+二分) 斯特林公式

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

题目大意:找出最小的正整数n使得n! > an

解题思路:我们两边取对数,就可以得到 log(n!) > n*log(a),接下来我们就可以二分查找求解,这里对于log(n!)有两种求法,一种是直接求出1e7内的所有结果,再用数组存起来(因为答案不会超出这个范围),然后二分搜索答案,或者用斯特林公式求出n!的近似值来比较。
斯特林公式: n! \approx \sqrt{2\pi n}\, \left(\frac{n}{e}\right)^{n}.  取对数即有:log(n!) = 0.5*log(2*pi*n) + n*(log(n) - 1)                      (pi = 3.14)

AC代码(前缀法):
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#define bug printf("***\n");
//#define mem0 memset(a, 0, sizeof(a));
using namespace std;
typedef pair<long long, int> par;
const int mod = 1e9+7;
const int INF = 1e7;
const int N = 1000010;
const double pi = 3.1415926;
using namespace std;

int T, a;
double sum[3000010];

int main()
{
    scanf("%d", &T);
    sum[0] = 0;
    for(int i = 1; i < 3000010; i ++) {
        sum[i] = sum[i-1]+log(i);
    }
    while(T --) {
        scanf("%d", &a);
        int l = 0, r = 3000010;
        double x = log(a);
        while(l+1 < r) {
            int mid = (l+r)/2;
            if(sum[mid] > mid*x) r = mid;
            else l = mid;
        }
        if(sum[l] > l*x) printf("d\n", l);
        else printf("%d\n", l+1);
    }
    return 0;
}
AC代码(斯特林公式):
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#define bug printf("***\n");
//#define mem0 memset(a, 0, sizeof(a));
using namespace std;
typedef pair<long long, int> par;
const int mod = 1e9+7;
const int INF = 1e7;
const int N = 1000010;
const double pi = 3.1415926;
using namespace std;

int T, a;

bool check(int x)
{
    if(0.5*log(2*pi*x)+x*(log(x)-1) > x*log(a))
        return true;
    return false;
}

int main()
{
    scanf("%d", &T);
    while(T --) {
        scanf("%d", &a);
        int l = 0, r = INF;
        while(l+1 < r) {
            int mid = (l+r)/2;
            if(check(mid)) r = mid;
            else l = mid;
        }
        if(check(l)) printf("d\n", l);
        else printf("%d\n", l+1);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值