HDU 4336 Card Collector

Link

meaning of the title

  Given n cards, each with a probability of appearing, find the expected number of times that all cards are collected.

ideas

  shape pressure dp. dp[i] represents the expected number of times that i have been collected. dp[n]=0; may transfer to yourself and solve the equation.

  Remember the bug of zz: 1. 1<<20 is 1048576 after calculating it in the calculator, and then I missed one digit, opened 110000, and then kept Runtime Error (ACCESS_VIOLATION). 2. Don't see that the sample is 3 digits, and then output 3 digits. . .

 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 }

 

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324991998&siteId=291194637