码迷,mamicode.com
首页 > 其他好文 > 详细

【HDOJ】4986 Little Pony and Alohomora Part I

时间:2014-08-31 22:37:21      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   art   div   log   

递推。设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,显然没用欧拉级数化简,化简后就过了。

 1 #include <cstdio>
 2 #include <cmath>
 3 
 4 #define MAXN 100000
 5 
 6 double a[MAXN];
 7 
 8 int main() {
 9     int n;
10     int i;
11     double ans;
12 
13     a[0] = 0;
14     for (i=1; i<MAXN; ++i)
15         a[i] = a[i-1]+1.0/i;
16 
17     while (scanf("%d", &n) != EOF) {
18         if (n < MAXN) {
19             ans = a[n];
20         } else {
21             ans = log(n*1.0)+0.57721566490153286060651209;
22         }
23         printf("%.4lf\n", ans);
24     }
25 
26     return 0;
27 }

 

【HDOJ】4986 Little Pony and Alohomora Part I

标签:style   blog   color   io   ar   for   art   div   log   

原文地址:http://www.cnblogs.com/bombe1013/p/3948195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!