标签:des style blog http color io os java ar
http://acm.hdu.edu.cn/showproblem.php?pid=4986
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 259 Accepted Submission(s): 143
题意:一些钥匙随机放在箱子里,现在问打开次数期望
思路:每种方式相当于一个置换的循环个数,那么考虑f[i]为i个箱子的情况,f[i + 1]要么就是放在最后多一个循环,要么就是插入中间循环个数不变,对应的转移为f[i + 1] = (f[i] + 1) / i + f[i] * (i - 1) / i 化简得到f[i] = f[i - 1] + 1 / i
这个式子i越大,越趋近lni + C,这个C为犹拉常数,所以先递推出数字小的情况,大的就直接计算
#include <iostream> #include <iomanip> #include <fstream> #include <sstream> #include <cassert> #include <algorithm> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <numeric> #include <bitset> #include <cstdio> #include <cstring> using namespace std; #define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; i++) #define fer(i, x, n) for (int i = (int)(x), _n = (int)(n); i < _n; i++) //////////////////////////////////////////////////////////////////////////////// const int N = 1000000; double f[N]; void init(){ f[1]=1; fer(i,2,N+1) f[i] = f[i-1] + 1.0/i; } int main() { //freopen("in.txt","r",stdin); ios_base::sync_with_stdio(0); int n; init(); while(scanf("%d",&n)!=EOF){ printf("%.4lf\n", n > N ? 0.57721566490153286060651209+log(n+0.0) : f[n] ); } return 0; }
HDU4986 (Little Pony and Alohomora Part I )
标签:des style blog http color io os java ar
原文地址:http://www.cnblogs.com/rewrite/p/3967066.html