HDU4336 Card Collector "the principle of tolerance and repulsion"

Source: Internet
Author: User

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=4336


Main topic:

Each pack of snacks has a card, a total of n different cards, the probability of obtaining the N cards is p[i] (1 <= i <= N).

What is the expectation of collecting all the cards.


Ideas:

PI indicates the probability of getting the first card and EI indicates the expectation of the card.

Suppose there are two cards now, by test instructions:

E1 = 1/p1,e2 = 1/p2,e12 (indicates the expectation of buying 1 or 2 of one packet) = 1/(P1+P2).

When we calculate E1 and E2, the E12 is repeated 2 times and should be subtracted once. According to the theorem of the tolerant repulsion:

E = E1 + e2-e12.

In the same vein, three cards:

E = E1 + E2 + e3-e12-e13-e23 + E123.

And so on, if the item is an odd number (the expectation of an odd card), the item is added when the expected items are calculated.

If the item is an even item 2 (the expectation of an even entry card), the item is subtracted.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;double p[50];int n;double Solve () {    double xh = 0;    for (int i = 1; i < (1 << N); ++i)   //traverse 2^n kind of situation  from 00 ... 01 to 11 ... One    {        double sum = 0;        int odd = 0;        for (int j = 0; j < N; ++j)      //For I (in each case), calculate the number of bits of the binary 1 for i        {            if ((1<<j) & i)      //i the right-to-left number corresponds to the first J-bit 1. c12/>{                odd++;          Number of bits in binary 1                sum + = p[j+1];  Will add up to the probability of 1 items.            }        }        if (odd & 1)//odd items plus, even items minus            xh + = 1/sum;        else            XH-= 1/sum;    }    return XH;} int main () {while    (~scanf ("%d", &n)    } {for        (int i = 1; I <= N; ++i)            scanf ("%lf", &p[i]);        printf ("%.6lf\n", Solve ());  Note LF and F    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU4336 Card Collector "the principle of tolerance and repulsion"

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.