HDU 5036 Explosion Probability expectation

Source: Internet
Author: User
Tags bitset cas printf

Test instructions: give n boxes. There are locks on every box. A key can only be opened with a lock, a lock has only one key. Of course, for some boxes that can't be opened with keys, we have to open them violently. Given the number of keys in each box and the specific ability to open that box, ask for the number of violent open boxes, so that all the boxes are opened.

Idea: Very much like the xiaodaobc of a certain way open the box problem. But because the problem is that each box will randomly put a key. This is expected to be a harmonic progression.

This problem is similar, we get first: violent open this box, can open those boxes. This can be done with Bitset for state compression.

Then, for each case, we consider how many ways, so that: violence to open certain cases, while opening the box. The expectation of violent opening of the box is the reciprocal of the number of programs. Then our expectation of each box is the sum of the expectations of the final opening of all boxes of violent open chests.

The code is as follows:

#include <cstdio> #include <bitset> #include <algorithm> #include <iostream> using namespace std

;

const int MAX = 1010;
int t,k,n,num;

Bitset<max> Key[max];
    int main (void) {//freopen ("Input.txt", "R", stdin);
    int cas = 1;
    scanf ("%d", &t);
        while (t--) {scanf ("%d", &n);
            for (int i = 0; i < N; ++i) {key[i].reset ();
        Key[i][i] = true;
            } for (int i = 0; i < N; ++i) {scanf ("%d", &k);
                for (int j = 0; j < K; ++j) {scanf ("%d", &num);
            Key[i][num-1] = true; 
                    }} for (int j = 0, J < N; ++j) for (int i = 0; i < n; ++i) if (Key[i][j])


        Key[i] |= key[j];
        Double ans = 0.0;
            for (int j = 0; j < N; ++j) {int cnt = 0;
            for (int i = 0; i < N; ++i) if (key[i][j]) cnt++; Ans + = 1.0/ Cnt
        printf ("%f\n", ans);
    } printf ("Case #%d:%.5f\n", Cas++,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.