FACVSPOW - Factorial vs Power 数学方法 + 二分
2017-02-20 14:08
351 查看
http://www.spoj.com/problems/FACVSPOW/
求解n! > a^n最小的整数n
对于有n!和a^n的东西,一般是取ln
然后就是求解
(ln(1) + ln(2) + .... + ln(n)) / n > ln(a)的最小整数n
发现左边的函数单调,所以可以预处理出来,右边最大值是ln(1e6)
所以预处理5e6个。
View Code
据说
ln(n!) ≈ (n * ln(n)) - n + (1 / 2 * ln(2 * PI * n));
求解n! > a^n最小的整数n
对于有n!和a^n的东西,一般是取ln
然后就是求解
(ln(1) + ln(2) + .... + ln(n)) / n > ln(a)的最小整数n
发现左边的函数单调,所以可以预处理出来,右边最大值是ln(1e6)
所以预处理5e6个。
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <assert.h> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL; #include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> #include <bitset> const int maxn = 5e6 + 20; double f[maxn]; void init() { for (int i = 1; i <= maxn - 20; ++i) { f[i] = (f[i - 1] * (i - 1) + log(i * 1.0)) / (i * 1.0); } // for (int i = 1; i <= 100; ++i) { // cout << f[i] << endl; // } } int a; double up; const double eps = 1e-12; bool bigger(double a, double b) { return a - b > eps; } void work() { scanf("%d", &a); up = log(a * 1.0); int be = 1, en = maxn - 20; while (be <= en) { int mid = (be + en) >> 1; if (bigger(f[mid], up)) { en = mid - 1; } else be = mid + 1; } printf("%d\n", be); } int main() { #ifdef local freopen("data.txt", "r", stdin); // freopen("data.txt", "w", stdout); #endif init(); int t; scanf("%d", &t); while (t--) work(); return 0; }
View Code
据说
ln(n!) ≈ (n * ln(n)) - n + (1 / 2 * ln(2 * PI * n));
相关文章推荐
- SPOJ FACVSPOW 数论+二分
- SPOJ 5161 FACVSPOW - Factorial vs Power(数论+二分)
- SPOJ FACVSPOW(数论+二分)
- Spoj-FACVSPOW Factorial vs Power
- SPOJ FACVSPOW - Factorial vs Power
- 安装SAPGUI后,再安装VS.Net出现冲突的解决方法
- 解决用VS.NET开发Web应用程序和Subversion的冲突 方法二
- VS不能加载web项目的解决方法
- 如果在安装了vs.net之后再装iis的话必须对iis进行注册,方法如下
- VS.NET常见错误及解决方法集锦
- vs.net 2005中引用webservice的简单方法
- VS.NET常见错误及解决方法集锦
- 《TVS2.0应用实录》005:Together for VS.NET 2.0 Bugs及解决方法(part 1)
- VS不能加载web项目的解决方法
- 在vs中不用接外挂就可以启动NUnit的方法
- 更改VS.NET 默认SCM Provider的方法
- 更改VS.NET 默认SCM Provider的方法
- (转载)vs.net 2005 数据集压缩方法__推荐
- vs.net console 使用 web.config 连接数据库的方法
- VS.NET 2003调用组件时总是不能指向到正确的路径的解决方法