SPOJ FACVSPOW - Factorial vs Power 二分

题目链接http://www.spoj.com/problems/FACVSPOW/

题目大意:给定底数a,问什么时候n! 超过 an. 1 <= t <= 100000    2 <= a <= 106

解题思路:等价于计算 使1*2*3*4*5*...*k / a > 1 的 k。直接计算必然溢出,上下同取log,然后就不怕溢出了。预处理log前N项和,二分check找。注意使用double。

代码:

 1 int a;
 2 double sum[maxn];
 3 
 4 int check(){
 5     int l = 1, r = 5000000;
 6     while(l < r){
 7         int mid = (l + r) >> 1;
 8         double up = sum[mid], dn = mid * log(a);
 9         if(up >= dn) r = mid;
10         else l = mid + 1;
11     }
12     return l;
13 }
14 void dowork(){
15     sum[0] = 0;
16     for(int i = 1; i < 5e6; i++) sum[i] = sum[i - 1] + log(i);
17 }
18 int main(){
19     dowork();
20     int t;
21     scanf("%d", &t);
22     while(t--){
23         scanf("%d", &a);
24         int ans = check();
25         printf("%d\n", ans);
26     }
27 }

题目:

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

 

 
posted @ 2017-08-21 16:33  EricJeffrey  阅读(228)  评论(2编辑  收藏  举报