http://acm.hdu.edu.cn/showproblem.php?pid=4336
Test instructions: n cards, every time you take a box, the probability of the card I in the box is p[i], the desired number of boxes to be opened for all cards (N<=20)
#include <cstdio> #include <cstring>using namespace std;const int n=22;int n;double p[n], F[1<<n];int Main () {while (~SCANF ("%d", &n)) {int all= (1<<n) -1;for (int i=0; i<n; ++i) scanf ("%lf", &p[i]); f[all]=0; for (int s=all-1; s>=0;--s) {double up=1, down=0;for (int i=0; i<n; ++i) {if (s& (1<<i)) continue;up+=f[s| ( 1<<i)]*p[i];d own+=p[i];} F[s]=up/down;} printf ("%f\n", F[0]);} return 0;}
Set $f[s]$ to indicate the desired number of boxes to be opened for the current card with status $s$:
Consider opening a box:
1, no card, the probability is: $1-\sum_{i} p[i]$; expected and: $ (1-\sum_{i} P[i]) f[s]$
2, card $i$ has been collected, the probability is: $p [i]$; expected and: $\sum_{i \in s} p[i]f[s]$
3, card $i$ not collected, the probability is: $p [i]$; expected and: $\sum_{i \notin s} p[i]f[s \cup \{i \}]$
So
$$
\begin{align}
F[s]
& = (1-\sum_{i} P[i]) F[s] + \sum_{i \in s} p[i]f[s] + \sum_{i \notin s} p[i]f[s \cup \{i \}] \ \
& = \frac{1 + \sum_{i \notin s} p[i]f[s \cup \{i \}]}{\sum_{i \notin s} p[i]}
\end{align}
$$
Then also notice, SPJ words precision must pay attention ah, do not only output a few = =, the best output several, you understand ... Then wa a few hair.
"HDU" Card Collector