Test instructions: There are n kinds of cards, eating snacks when you will eat some cards, tell you in a bag of snacks to eat each card of the probability, to collect all the cards need to buy a snack bag number of expectations.
Idea: First state compression, then probability DP
The state transition equation is d[i] = P ' * (d[i]+1) + sigma (d[i |, using d[i] to indicate how many packets need to be bought by the state I to the target. (1 << j) * P[i]), then the same item is moved to the left and the answer is finally available.
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> # include<iostream> #include <algorithm> #include <vector> #include <map> # include<queue> #include <stack> #include <string> #include <map> #include <set># Define EPS 1e-6 #define LL long Long #define PII pair<int,int>using namespace std; const int MAXN = + 5;//const int INF = 0x3f3f3f3f;//freopen ("Input.txt", "R", stdin);d ouble p[25];d ouble d[1<< 21];int Main () {int n;while (scanf ("%d", &n) = = 1) {Double no = 1; for (int i = 0; i < n; i++) {scanf ("%lf", &p[i ]); no-= P[i];} int s = (1 << n) -1;d[s] = 0;for (int i = s-1; I >= 0; i--) {Double TP = no;d[i] = no;for (int j = 0; J < N; j + +) {if (1<<j) &i) {TP + p[j];d [i] + = P[j];} else D[i] + = p[j]* (d[i| ( 1<<J)]+1);} D[i] = d[i]/(1-TP);} printf ("%.6lf\n", D[0]);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 4336 Card Collector (probabilistic DP)