HDU 4336 Card Collector

链接

题意

  给n张卡片,每张卡片都有出现的概率,求收集到全部的卡片的期望次数。

思路

  状压dp。dp[i] 表示收集了i张,的期望次数。dp[n]=0;可能自己转移到自己,解一下方程。

  记zz的bug:1、1<<20在计算器中算完了之后是1048576,然后就少看了一位,开了110000,然后就一直 Runtime Error (ACCESS_VIOLATION)。2、千万不要看到样例是3位,然后就输出了3位。。。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<cmath>
 6 
 7 using namespace std;
 8 
 9 const int N = (1<<21)+10;
10 
11 double p[30],dp[N];
12 
13 int main() {
14     int n;
15     while (~scanf("%d",&n)) {
16         for (int i=1; i<=n; ++i) scanf("%lf",&p[i]);
17         int t = (1 << n) - 1;
18         dp[t] = 0;
19         for (int s=t-1; s>=0; --s) {    
20             double tmp = 0,p0 = 0;
21             for (int i=1; i<=n; ++i) {
22                 if (s & (1<<(i-1))) continue;
23                 tmp += dp[s | (1<<(i-1))] * p[i];
24                 p0 += p[i];
25             }
26             dp[s] = (tmp+1.0)/p0;
27         }
28         printf("%lf\n",dp[0]);
29     }
30     return 0;
31 }

  

猜你喜欢

转载自www.cnblogs.com/mjtcn/p/8966410.html