Hdu-4649-worker sor Tian

Source: Internet
Author: User

--> The operand range is <2 ^ 20, which indicates bitwise operation. each operand can be converted into a binary number of up to 20 bits, after the number of n + 1 Operations is obtained, the probability of 1 appears for each binary bit (the probability of 0 does not need to be calculated. If this bit is 0, the total number is irrelevant to this bit, only occupies space ).

Set a [I] [j] to represent the j-th binary bit of the I-th number; p [I] [j] to represent the bit of the binary, after the first I count operation, the probability of j is obtained (j is 0 or 1). d [I] indicates the probability that the I th binary bit appears 1 after the n + 1 count operation.

For p [I] [j], the state transition equation is as follows:

P [I] [1] = p [I-1] [1] * P [I] + probabilities produced by various computations.

P [I] [0] = 1-p [I] [1].

 

#include <cstdio>using namespace std;const int maxn = 200 + 3;const int maxm = 20 + 3;int A[maxn], a[maxn][maxm], n;char O[maxn];double P[maxn], p[maxn][2], d[maxm];void read(){    int i;    for(i = 0; i < n+1; i++) scanf("%d", &A[i]);    for(i = 1; i <= n; i++){        getchar();        O[i] = getchar();    }    for(i = 1; i <= n; i++) scanf("%lf", &P[i]);}void init(){    int i, j;    for(i = 0; i < n+1; i++)        for(j = 0; j < 20; j++) a[i][j] = (1 << j) & A[i];}void dp(){    int i, bit;    for(bit = 0; bit < 20; bit++){        if(a[0][bit]){            p[0][1] = 1;            p[0][0] = 0;        }        else{            p[0][0] = 1;            p[0][1] = 0;        }        for(i = 1; i < n+1; i++){            p[i][1] = p[i-1][1] * P[i];            switch(O[i]){                case '&':{                    if(a[i][bit]) p[i][1] += p[i-1][1] * (1 - P[i]);                    break;                }                case '|':{                    if(a[i][bit]) p[i][1] += 1 - P[i];                    else p[i][1] += p[i-1][1] * (1 - P[i]);                    break;                }                case '^':{                    if(a[i][bit]) p[i][1] += p[i-1][0] * (1 - P[i]);                    else p[i][1] += p[i-1][1] * (1 - P[i]);                    break;                }            }            p[i][0] = 1 - p[i][1];        }        d[bit] = p[n][1];    }}double solve(){    double ret = 0;    for(int i = 0; i < 20; i++) ret += (1 << i) * d[i];    return ret;}int main(){    int kase = 1;    while(scanf("%d", &n) == 1){        read();        init();        dp();        printf("Case %d:\n%.6lf\n", kase++, solve());    }    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.