SPOJ FACVSPOW 数论+二分

26 篇文章 0 订阅
12 篇文章 0 订阅

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 thang(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!>a^n 最小的n。


题解:

n*(n-1)*...*2*1>a*a*a...*a

ln(n)+ln(n-1)+...+ln(2)+ln(1)>ln(a)+ln(a)...+ln(a)

ln(a)<(ln(n)+ln(n-1)+...+ln(1))/n

预处理完二分查找


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
double pre[3000005];
int main(){
	int n,t,i;
	double now=0;
	for(i=1;i<=3000000;i++){
		now+=log(i);
		pre[i]=now/i;
	}
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		int l=1,r=3000000;
		double d=log(n);
		while(l+1<r){
			int mid=l+r>>1;
			if(d<pre[mid])r=mid;
			else l=mid;
		}
		if(d<pre[l])printf("%d\n",l);
		else printf("%d\n",r);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值