[HDU 4336] card collection [State compression DP] [probability DP] [refresh principle]

Source: Internet
Author: User

Question:

There are N types of cards in snacks. The probability of each card I is pi. A bag of snacks may not have a card, but there is a maximum of one card.

Ideas:

1. DP, DP [s] indicates that the expected number of bags is collected in the S state.

S = 11111 indicates the status of the set when n = 5. In this case, DP [s] = 0;

Pay attention to the expected question. The definition of DP is generally changed from the final state to the initial state, that is, reverse seeking.

Because "expectation" is

Determine the event result * the probability of the event occurrence = average. The expected result is

If a card is transferred to S2 in the S1 state, S2 is a definite state, and there are multiple possibilities in the S1 state. Therefore, we can understand the rationality of the reverse request.

The final state is the "destination" of the initial state, which is indeed the expected "Source ".

// 281ms8480k # include <cstdio> 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 that the probability of no card is DP [(1 <n)-1] = 0; // you do not need to purchase it again after all the data is collected. expectation is generally reversed. for (INT I = (1 <n)-2; I> = 0; I --) // traverse all schemes {Double X = 0, sum = 1; /// take your card for (Int J = 0; j <n; j ++) {If (I & (1 <j ))) X + = P [J]; // if this card already exists in I, cumulative probability else sum + = P [J] * DP [I | (1 <j)]; // if not, it can be converted from this situation // DP [I | (1 <j)] is "Event", P [J] is the probability of event occurrence, multiply indicates expected .} DP [I] = sum/(1-tt-x);} printf ("%. 5lf \ n ", DP [0]);} return 0 ;}

Repeat it by yourself:

// 250 ms 8480 K # include <cstdio> using namespace STD; const int maxn = 22; Double P [maxn], DP [1 <maxn]; int main () {int n, m; while (scanf ("% d", & n) = 1) {for (INT I = 0; I <n; I ++) scanf ("% lf", P + I); M = 1 <n; DP [M-1] = 0; For (INT I = m-2; I> = 0; I --) {double sump = 0, sum = 1; for (Int J = 0; j <n; j ++) {If (! (I & (1 <j) // bit operations are written into logic and... hand disability {sump + = P [J]; // sum + = P [J] * DP [I | (1 <j)];} DP [I] = sum/sump;} printf ("%. 5lf \ n ", DP [0]); // although the output in the sample is 3 bits, the description in the question is 1-4 error... so ...}}

2. Record the review principle (write down the record and learn later ...)

// 421ms340k # include <iostream> # include <cstdio> using namespace STD; double S; int N, vis [25]; Double A [25]; void DFS (int K, double sum, int cou, Int J) {If (COU = k) {S + = 1/SUM; return ;}for (INT I = J; I <= N; I ++) {sum + = A [I]; cou ++; DFS (K, sum, cou, I + 1); cou --; sum-= A [I];} return;} int main () {While (~ Scanf ("% d", & N) {for (INT I = 1; I <= N; I ++) {scanf ("% lf ", & A [I]);} double sum = 0; For (INT I = 1; I <= N; I ++) sum + = (1/A [I]); For (INT I = 2; I <= N; I ++) {S = 0; DFS (I, 0, 0, 1); if (I % 2 = 0) sum + = (-1) * s; else sum + = s;} printf ("% lf \ n ", sum) ;}}/* HDU 4336 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.