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

时间: 2024-11-05 14:31:12

Spoj-FACVSPOW Factorial vs Power的相关文章

SPOJ FCTRL - Factorial

题目链接:http://www.spoj.com/problems/FCTRL/ 题目大意:询问N的阶乘的结果尾数有几个0. 解题思路:考虑问题:N的阶乘的结果能被2m整除,这个m最大为多少. 我们对前N个数除以2,忽略奇数,会得到N/2个数字.那么相当于我们得到了2N/2 对之后的N/2个数字继续除以2,同样忽略奇数.我们会再得到2N/4 ... 所以m=N/2 + N/4 + N/8 +...+0. 那么对于这个问题,我们计算出N的记成被2m1整除的最大m1以及被5m2整除的最大m2,由于2

Factorial vs Power

题意 输入a,找到满足n!>a^n 最小的n. 数据 第一行T(1 <= T <= 1e5),表示测试样例数.(2 <= a <= 1e6). 输入 3 2 3 4 输出 4 7 9 这个东西一看就知道是二分求解的,但是我们还是不知道怎么求的,我们可以吧他们取对数然后我们很惊奇的发现原来真的是取对数啊, 最后原来的式子就变成了这个样子... [ln(1)+ln(2)+ln(3)+....+ln(n)]>n*lna 然后因为ln(n)为正数所以我们就把ln(n)除过去,大

SPOJ Python Day1: Factorial

11. Factorial 这个题同样非常简单,就是求一个数的阶乘的尾部有多少个0. 思路是有2*5才会出0,然后2肯定比5多,所以就是数N!中有多少个因子5. 关于如何数出因子5的个数中http://www.chinaunix.net/old_jh/23/926848.html这篇文章介绍的非常详细.我就不谈了,不过想说写程序和算法是两个非常不同的工作,我现在的目标是,大概看一下前人的成法,主要完成编程工作. 最终推出的计算公式为: 当$0 < n < 5$时,$f(n!) = 0$; 当$

SPOJ:Divisors of factorial (hard) (唯一分解&amp;优化)

Factorial numbers are getting big very soon, you'll have to compute the number of divisors of such highly composite numbers. Input The first line contains an integer T, the number of test cases.On the next T lines, you will be given two integers N an

check a int number is power of 2?

个人信息:就读于燕大本科软件工程专业 目前大三; 本人博客:google搜索"cqs_2012"即可; 个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献; 博客内容:how many does the factorial of n have zero? 博客时间:2014-5-7; 编程语言:Java ; 编程坏境:Windows 7 专业版 x64; 编程工具:jdk,eclipse x64; 制图工具:office 2010 ppt; 硬件信息:7G-3 笔记

LightOJ 1035 Intelligent Factorial Factorization

题目: Description Given an integer N, you have to prime factorize N! (factorial N). Input Input starts with an integer T (≤ 125), denoting the number of test cases. Each case contains an integer N (2 ≤ N ≤ 100). Output For each case, print the case num

2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 思路: 我的思路:新建一个数组存放旋转后的内容,但是怎么把原数组的内容存放在数组中,不清楚. leetcode/discuss思路: 思路一:新建数组,复制原

[LeetCode] Factorial Trailing Zeros

Well, to compute the number of trailing zeros, we need to first think clear about what will generate a trailing 0? Obviously, a number multiplied by 10 will have a trailing 0 added to it. So we only need to find out how many 10's will appear in the e

MOOCULUS微积分-2: 数列与级数学习笔记 6. Power series

此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 Sequences and Series 本系列学习笔记PDF下载(Academia.edu) MOOCULUS-2 Solution Summary Let $(a_n)$ be a sequence of real numbers starting with $a_0$. Then the power