HDU 4,336 Integrated instant noodle card expectations-expected DP or repulsion principle-(binary compression assist)

Source: Internet
Author: User

Test instructions: To set up n kinds of cards, it is now known that every opening of a bag of instant noodles to get the probability of each card pi,pi and less than equal to 1, to seek to set this n cards need to buy instant noodles expectations.

Analysis:

First, expect DP, this problem to be used in binary compression, which is very obvious.

Status: Dp[i] I have now collected I cards to reach the expectation of collecting all cards. This state can be transferred to a state that has: 1). Dp[i] (the card that was received for the next instant noodle is already collected); 2). dp[i| ( 1<<J)] (the next time you get a card that has not been collected)

So the equation: Dp[i]=pi * dp[i] + Pj * dp[i| ( 1<<J)] + 1.0(don't forget +1) where pi is the probability of all the cards that have been collected, plus the probability that no cards are in the instant noodles, that is, the opposite of the cards that are not collected.

Implementation: From the back forward, from the last state of the previous start to push forward, the result is output DP[0]

Second, the principle of tolerance and repulsion

1/PI is to see the first card alone, to collect its expectations, but it is wrong to add all the individual expectations of the cards, because their expectations have a cross-section, so to subtract 1/(PI+PJ), well understood, that is, two overlapping parts of the set, then why is the sum it up? Analysis of this overlapping part: should be a must not B, or b not a, because it is impossible to get a and B, so the probability of the two cases just now is PA+PB, and so on.

DP Code:

#include <iostream> #include <cstdio>using namespace Std;int n;double p[30],dp[1<<21];int main () { while (cin>>n) {double q=1.0;for (int i=0;i<n;i++) {cin>>p[i];q-=p[i];} Memset (Dp,0,sizeof (DP)), for (int i= (1<<n) -2;i>=0;i--) {double pp=q;for (int j=0;j<n;j++) {if (i& (1< <J)) Pp+=p[j];else dp[i]+=p[j]*dp[i| ( 1<<J)];} Dp[i]+=1.0;dp[i]/= (1.0-PP);} printf ("%f\n", Dp[0]);}}
Tolerance Principle Code:

#include <iostream> #include <cstdio>using namespace Std;int main () {int n;double a[30];while (cin>>n) {double s=0;for (int i=0;i<n;i++) cin>>a[i];for (int i=1;i< (1<<n); i++) {     double sum=0; int tot=0; for (int j=0;j<n;j++) {if (i& (1<<j)) {tot++; sum+=a[j];  }} if (tot&1) {s+=1.0/sum;} else s-=1.0/sum;}    printf ("%lf\n", s);}}



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

HDU 4,336 Integrated instant noodle card expectations-expected DP or repulsion principle-(binary compression assist)

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.