SPOJ FACVSPOW - Factorial vs Power
2017-02-23 17:15
429 查看
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 <= 1000002 <= 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! > an
最小的n
取对数,然后发现[ln(1)+ln(2)+ln(3)+ln(4)+ ……+ln(n)]/n 和n是线性关系的,所以可用二分来求满足>n*ln(a)的最小n
哦对学到了一个很有趣的斯特林公式
ln(n!) = n * ln(n) - n + 0.5*(ln(PI * 2 * n))
#include<cmath> #include<algorithm> #include<cstring> #include<string> #include<set> #include<map> #include<time.h> #include<cstdio> #include<vector> #include<stack> #include<queue> #include<iostream> using namespace std; #define LONG long long const int INF=0x3f3f3f3f; const int MOD=1e9+7; const double PI=acos(-1.0); #define clrI(x) memset(x,-1,sizeof(x)) #define clr0(x) memset(x,0,sizeof x) #define clr1(x) memset(x,INF,sizeof x) #define clr2(x) memset(x,-INF,sizeof x) #define EPS 1e-10 bool check(LONG n ,double a ) { double x = (double )n; double sum = x * log(x) - x + 0.5*(log(PI * 2 * x)); if(sum > x * log(a)) return 1; else return 0; } int main() { int T; cin>>T; while(T--) { double a ; cin>>a ; LONG l = 1 , r=10*a; LONG mid ; while(l < r) { mid = (l + r ) / 2; if(check(mid,a)) { r = mid ; } else l = mid + 1; // cout<<mid<<endl; } cout<<l<<endl; } }
相关文章推荐
- SPOJ 5161 FACVSPOW - Factorial vs Power(数论+二分)
- Spoj-FACVSPOW Factorial vs Power
- SPOJ FACVSPOW 数论+二分
- SPOJ FACVSPOW(数论+二分)
- FACVSPOW - Factorial vs Power 数学方法 + 二分
- ACM 斯特林公式 Factorial vs Power
- Factorial vs Power
- SPOJ_TBATTLE:Thor_vs_Frost_Giants(数论+二分)
- 【SPOJ】Power Modulo Inverted(拓展BSGS)
- SPOJ:Harbinger vs Sciencepal(分配问题&不错的DP&bitset优化)
- Single Number && Single Number II && Factorial Trailing Zeroes && Pow(x, n) && Add Two Numbers
- SPOJ Python Day1: Factorial
- VS中pow的使用
- PowerDesigner Vs Enterprise Architect
- VS2015安装EF Power Tools
- New Version of the VS 2010 Productivity Power Tools is Available
- SPOJ:Divisors of factorial (hard) (唯一分解&分块优化)
- VS插件,好用,提高代码效率。Productivity Power Tools
- 在 VS14 上安装 Visual F# Power Tools
- VS 插件 Productivity Power Tools - 更改 选项卡组件位置