Hdu 4336 Card Collector (probability dp + bit operation expectation)

Source: Internet
Author: User

Question link

Card Collector

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 2711 Accepted Submission (s): 1277
Special Judge


Problem DescriptionIn your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, for example, if you collect all the 108 people in the famous novel Water Margin, you will win an amazing award.

As a smart boy, you notice that to win the award, you must buy much more snacks than it seems to be. to convince your friends not to waste money any more, you should find the expected number of snacks one shoshould buy to collect a full suit of cards.

 

InputThe first line of each test case contains one integer N (1 <= N <= 20), indicating the number of different cards you need the collect. the second line contains N numbers p1, p2 ,..., pN, (p1 + p2 +... + pN <= 1), indicating the possibility of each card to appear in a bag of snacks.

Note there is at most one card in a bag of snacks. And it is possible that there is nothing in the bag.

 

OutputOutput one number for each test case, indicating the expected number of bags to buy to collect all the N different cards.

You will get accepted if the difference between your answer and the standard answer is no more that 10 ^-4.

 

Sample Input10.120.1 0.4

 

Sample Output10.00010.500

Question:

There are N (1 <= N <= 20) cards. The probability of containing these cards in each package is p1, p2, ''' pN.
Each package can contain up to one card, and there may be no card.
Find the number of packages required to get the N cards and the expected number of times.

Analysis:

N is 20, 2 ^ 20 =1 576;

Therefore, each digit can be used to indicate whether or not such a card exists or not.

Inverse formula:

D [I] = 1.0 + d [I] * p2 + d [I | (1 <j)] * p [j];

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cstdlib> 5 # include <queue> 6 # include <cmath> 7 # include <algorithm> 8 # define LL _ int64 9 const int maxn = (1 <20) + 10; 10 using namespace std; 11 double d [maxn], p [30]; 12 13 int main () 14 {15 int n, I, j; 16 double sum, tmp; 17 while (~ Scanf ("% d", & n) 18 {19 memset (d, 0, sizeof (d); 20 sum = 0; 21 for (I = 0; I <n; I ++) 22 {23 scanf ("% lf", & p [I]); 24 sum + = p [I]; 25} 26 tmp = 1.0-sum; 27 d [(1 <n)-1] = 0; 28 for (I = (1 <n)-2; i> = 0; I --) 29 {30 double p2 = tmp, p3 = 0; 31 for (j = 0; j <n; j ++) 32 {33 if (I & (1 <j) 34 p2 + = p [j]; // p2 indicates the probability that no new card is drawn and 35 else36 p3 + = p [j] * d [I | (1 <j)]; 37} 38 d [I] + = (1.0 + p3)/(1.0-p2); 39} 40 printf ("%. 4lf \ n ", d [0]); 41} 42 return 0; 43}

 



 

Hdu 4336 Card Collector (probability dp + bit operation expectation)

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.