Factorial vs Power
来源:互联网 发布:常见文本文件格式知乎 编辑:程序博客网 时间:2024/06/06 09:46
Consider two integer sequences f(n) = n! and g(n) = an, wheren 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 integerk, f(n) is greater than g(n) for alln >= k. You are to find the least positive value of n for whichf(n) > g(n), for a given positive integer a > 1.
Input
The first line of the input contains number t – the amount of tests. Thent test descriptions follow. Each test consist of a single numbera.
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:3234Output:479
对于给定的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<cstdio>#include<cmath>#include<algorithm>#define PI acos(-1)#define ll long long using namespace std;bool check(ll n, int a) {double s = (double)n;double sum = s * log(s) - s + 0.5*(log(PI * 2 * s));if (sum > s*log(a)) return true;return false;}int main() {int T;scanf("%d", &T);double a;while (T--) {scanf("%lf", &a);ll l = 0, r = 99999999;ll mid;while (l < r) {mid = (l + r) / 2;if (check(mid, a)) {r = mid;}else l = mid+1;}printf("%lld\n", l);}}
阅读全文
0 0
- Factorial vs Power
- ACM 斯特林公式 Factorial vs Power
- Factorial Power(阶乘最后非零位)
- 浅谈Power"s"家族史--Power Povit vs. Power Query vs. Power View vs. Power BI
- Power BI vs. QuickBI vs. DataV
- Factorial!
- Factorial
- Factorial
- factorial
- Factorial
- Factorial
- Factorial
- Factorial
- Factorial
- Factorial
- Factorial
- Factorial
- Factorial
- javascript中的标签语句label:statement
- Rocket简介(1)
- Java的数据类型
- BZOJ 4720 [Noip2016]换教室——期望DP
- wamp多版本php的配置
- Factorial vs Power
- BSOI_2262.遥控车 (car.pas/c/cpp)
- JVM内存模型(深入理解Java虚拟机学习笔记)
- hibernate框架一之基本知识及编写一个基本的项目
- codeforces 808D
- @Autowired与@Resource的区别
- java开发命名规范总结
- Python的RSA
- 学习curses图形库(四)