Card Collector Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=4336
Test instructions
Go to the store to buy snacks, in a pack of snacks up to 1 cards, it may not be, ask to collect all the N (n≤20) cards need to buy the number of snacks expected.
Exercises
Set Dp[i] (binary, corresponding bit to 1 indicates that the card already has) for the current state as the starting point also need to purchase the number of snacks to expect, then dp[0] is the answer
There is a problem with the DP solver, so look here.
Code
#include <stdio.h>
#include <string.h>
const int N=20;
Double dp[1<<n],p[n],x;
int main ()
{
int n;
while (~SCANF ("%d", &n))
{
dp[(1<<n) -1]=0.0;
for (int i=0;i<n;++i)
scanf ("%lf", &p[i]);
for (int j= (1<<n) -2;j>=0;--j)
{
x=0;dp[j]=0.0;
for (int k=0;k<n;++k)
{
if (! ( (1<<k) &j))
dp[j]+=dp[j+ (1<<k)]*p[k],x+=p[k];
}
dp[j]= (dp[j]+1.0)/x;
}
printf ("%.5f\n", dp[0]);
}
}
HDU 4336:card Collector expectation + shape pressure