【HDOJ】4986 Little Pony and Alohomora Part I
2014-08-31 21:30
274 查看
递推。设n个盒子的Spell次数为S(n),期望为E(n)。
当有n个盒子时,可能第n把钥匙在第n个盒子中,此时的Spell次数应该为(n-1)!+S(n-1);
当第n把钥匙不在第n个盒子中,混合排列,此时的Spell次数为(n-1)*S(n-1),
因此,期望E(n) = S(n)/n!,S(n) = (n-1)!+S(n-1) + (n-1)*S(n-1) = (n-1)!+n*S(n-1),
则E(n) = S(n-1)/(n-1)! + 1/n = E(n-1) + 1/n。
因此,得到递推公式E(n) = 1+1/2+1/3...1/n。
调和计数,第一次交TLE,显然没用欧拉级数化简,化简后就过了。
当有n个盒子时,可能第n把钥匙在第n个盒子中,此时的Spell次数应该为(n-1)!+S(n-1);
当第n把钥匙不在第n个盒子中,混合排列,此时的Spell次数为(n-1)*S(n-1),
因此,期望E(n) = S(n)/n!,S(n) = (n-1)!+S(n-1) + (n-1)*S(n-1) = (n-1)!+n*S(n-1),
则E(n) = S(n-1)/(n-1)! + 1/n = E(n-1) + 1/n。
因此,得到递推公式E(n) = 1+1/2+1/3...1/n。
调和计数,第一次交TLE,显然没用欧拉级数化简,化简后就过了。
#include <cstdio> #include <cmath> #define MAXN 100000 double a[MAXN]; int main() { int n; int i; double ans; a[0] = 0; for (i=1; i<MAXN; ++i) a[i] = a[i-1]+1.0/i; while (scanf("%d", &n) != EOF) { if (n < MAXN) { ans = a ; } else { ans = log(n*1.0)+0.57721566490153286060651209; } printf("%.4lf\n", ans); } return 0; }
相关文章推荐
- 【数论】 HDOJ 4986 Little Pony and Alohomora Part I
- hdu 4986 Little Pony and Alohomora Part I(递推)
- 【CUGBACM15级BC第7场 B】hdu 4986 Little Pony and Alohomora Part I
- HDU 4986 Little Pony and Alohomora Part I(递推+犹拉常数)
- BestCoder7 1002 Little Pony and Alohomora Part I(hdu 4986) 解题报告
- HDU 4986 Little Pony and Alohomora Part I(递推+欧拉常数)
- hdu 4986 Little Pony and Alohomora Part I(找规律,欧拉常数)
- 杭电 bestcoder round#7 1002 Little Pony and Alohomora Part I
- HDU4986 Little Pony and Alohomora Part I
- 【强连通分量】 HDOJ 4985 Little Pony and Permutation
- 【HDOJ】4985 Little Pony and Permutation
- Codeforces Round #259 (Div. 1) A. Little Pony and Expected Maximum 数学公式结论找规律水题
- HDU 4985-Little Pony and Permutation(模拟置换)
- Codeforces Round #259 (Div. 2) A. Little Pony and Crystal Mine
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift(模拟)
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift(序列)
- B - Little Pony and Sort by Shift
- 【CF】259 Div.1 B Little Pony and Harmony Chest
- Codeforces Round #259 (Div. 1) B. Little Pony and Harmony Chest(状压dp)
- 【HDU】4985 Little Pony and Permutation 置换群