首页 > 代码库 > Spoj-FACVSPOW Factorial vs Power

Spoj-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

 

有很多组询问,给个常数1<=a<=100w,求使得n! > a^n 的最小整数n

构造f(n)=n!,g(n)=a^n,a是常数,由高中知识就很容易知道f(n)趋近极限的速度最后会更快

不妨令h(n)=f(n)-g(n),则h(n)应当是递增的(吧?)

只要求h(n)=(n!-a^n) > 0的最小n

因此可知当a增加的时候,h(n)的零点应当也是增加的

所以可以枚举个a的值,不断增加n的值,只要n!>a^n,即log(n!)>nloga

即log1+log2+...+logn>nloga

左边的部分可以在枚举a的时候顺便求得

技术分享
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<deque>
 9 #include<set>
10 #include<map>
11 #include<ctime>
12 #define LL long long
13 #define inf 0x7ffffff
14 #define pa pair<int,int>
15 #define mkp(a,b) make_pair(a,b)
16 #define pi 3.1415926535897932384626433832795028841971
17 using namespace std;
18 inline LL read()
19 {
20     LL x=0,f=1;char ch=getchar();
21     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
22     while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
23     return x*f;
24 }
25 int n;
26 int ans[1000010];
27 int main()
28 {
29     double s=0;int t=1;
30     for (int i=1;i<=1000000;i++)
31     {
32         while (s<=t*log(i)){t++;s+=log(t);}
33         ans[i]=t;
34     }
35     int T=read();
36     while (T--){printf("%d\n",ans[read()]);}
37 }
Spoj FACVSPOW

 

Spoj-FACVSPOW Factorial vs Power