There are n kinds of cards. Some cards will be used when you eat snacks, telling you to eat each card in a bag of snacks.
The probability of buying snacks for each card.
Analysis: n is relatively small, it is natural to think of State compression DP
Analyze the recursive formula of the transfer process.
Assume that the number of digits in the S state is 1, indicating that the card has not been obtained.
Get one of these cards,
You may also get the cards you have obtained,
Another card may not be available.
The status of the last two cases remains unchanged.
Dp [0] = 0; (indicating that each card is finished, and the expected value is 0)
Dp [S] = sum * dp [S] + p [x1] dp [S ^ (1 <x1)] + p [x2] dp [S ^ (1 <x2)]... + 1;
Sum is the sum of probabilities in the last two cases.
To get the expression of dp [S ].
Finally, the output dp [(1 <n)-1] indicates that every card has no expectations.
[Cpp]
# Include <cstdio>
# Include <cstring>
Double dp [1 <20];
Double p [22];
Int main ()
{
Int n;
While (scanf ("% d", & n )! = EOF)
{
Double x = 0;
For (int I = 0; I <n; I ++)
{
Scanf ("% lf", & p [I]);
X + = p [I];
}
Dp [0] = 0;
For (int I = 1; I <(1 <n); I ++)
{
Double cnt = 0;
Double sum = 0;
Double s = 0;
For (int j = 0; j <n; j ++)
{
If (! (I & (1 <j )))
{
Sum + = p [j];
}
If (I & (1 <j ))
{
S + = p [j] * dp [I ^ (1 <j)];
}
}
Sum + = _x;
Dp [I] = (s + 1)/(1-sum );
}
Printf ("%. 5lf \ n", dp [(1 <n)-1]);
}
Return 0;
}
Author: haha593572013