Hdu 4336 dp expectation (State compression)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.