HDU 4336 card collector (probability DP, State compression)

Source: Internet
Author: User
Card collector

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1405 accepted submission (s): 624
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 input1 0.1 2 0.1 0.4

 

Sample output10.00010.500

 

Source2012 multi-university training contest 4

 

Recommendzhoujiaqi2010 State compression probability DP, or the principle of rejection,
 /*  HDU 4336 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. It can be done using the principle of rejection. You can also perform State compression for the expected DP probability.  */  # Include <Stdio. h> # Include <Algorithm> # Include <Iostream> # Include < String . H> Using  Namespace  STD;  Const   Int Maxn = 22  ;  Double  P [maxn];  Double DP [ 1 < Maxn];  Int  Main (){  Int  N;  While (Scanf ("  % D  " , & N )! = EOF ){  Double Tt = 0  ;  For ( Int I = 0 ; I <n; I ++ ) {Scanf (  "  % Lf  " ,& P [I]); TT + = P [I];} TT = 1 -TT; //  TT indicates the probability that no card is available. DP [( 1 <N )- 1 ] = 0  ;  For ( Int I = ( 1 <N )- 2 ; I> = 0 ; I --){  Double X = 0 , Sum = 1  ;  For ( Int J = 0 ; J <n; j ++ ){  If (I &( 1 <J) x + = P [J];  Else Sum + = P [J] * DP [I | (1 < J)];} DP [I] = Sum /( 1 -TT- X);} printf (  "  %. 5lf \ n  " , DP [ 0  ]);}  Return   0  ;} 

 

 /*  HDU 4336 anti-DDoS principle bit element Enumeration  */ # Include <Stdio. h> # Include < String . H> # Include <Iostream> # Include <Algorithm> Using   Namespace  STD;  Double P [ 22  ];  Int  Main (){  Int N;  While (Scanf ( "  % D  " , & N) = 1  ){  For ( Int I = 0 ; I <n; I ++) scanf ( "  % Lf  " ,& P [I]);  Double Ans =0  ;  For ( Int I = 1 ; I <( 1 <N); I ++ ){  Int CNT = 0  ;  Double Sum = 0  ;  For ( Int J = 0 ; J <n; j ++ )  If (I &( 1 < J) {sum + = P [J]; CNT ++ ;}  If (CNT & 1 ) Ans + = 1.0 / SUM;  Else Ans-=1.0 / SUM;} printf (  "  %. 5lf \ n  "  , ANS );}  Return   0  ;} 

 

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.