HDU 4336--card Collector —————— "Probability dp"

Source: Internet
Author: User

Card Collector

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3407 Accepted Submission (s): 1665
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'll win an amazing a Ward.

As a smart boy, you notice this to win the award, you must buy much more snacks than it seems to be. To convince your friends don't to waste money any more, you should find the expected number of snacks one should buy to Coll ECT a full suit of cards.

Inputthe first line of all test case contains one integer n (1 <= n <=), indicating the number of different card s 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 the most one card in a bag of snacks. And it is possible this 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 C Ards.

You'll get accepted if the difference between your answer and the standard answer are no more that 10^-4.

Sample Input10.120.1 0.4

Sample Output10.00010.500

Source2012 multi-university Training Contest 4 The main idea: to collect the characters in the instant noodles card, n is to collect n cards, the following gives the probability of n cards, asked you to collect all n card expectations. Problem solving idea: probability DP.
#include <bits/stdc++.h>using namespace Std;const int maxn=1<<21;double DP[MAXN];d ouble p[21];int Main () {    int n;    while (scanf ("%d", &n)!=eof) {for        (int i=0;i<n;i++)            scanf ("%lf", &p[i]);        dp[(1<<n) -1]=0;        for (int s= (1<<n) -2;s>=0;s--) {            double sum=1.0,sump=0;            for (int j=0;j<n;j++) {                if (!) ( (1<<j) &s) {                    sum+=dp[s| ( 1<<J)]*p[j];                    SUMP+=P[J];                }            }            dp[s]=sum/sump;        }        cout<<dp[0]<< "++++" <<endl;        printf ("%.5f\n", Dp[0]);    }    return 0;}

  

HDU 4336--card Collector —————— "Probability dp"

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.