HDOJ 4649 Professor Tian


按位DP。。。。。

Professor Tian

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 661    Accepted Submission(s): 360


Problem Description
Timer took the Probability and Mathematical Statistics course in the 2012, But his bad attendance angered Professor Tian who is in charge of the course. Therefore, Professor Tian decided to let Timer face a hard probability problem, and announced that if he fail to slove the problem there would be no way for Timer to pass the final exam. 
As a result , Timer passed. 
Now, you, the bad guy, also angered the Professor Tian when September Ends. You have to faced the problem too. The problem comes that there is an expression and you should calculate the excepted value of it. And the operators it may contains are '&' (and),'|'(or) and '^'(xor) which are all bit operators. For example: 7&3=3, 5&2=0, 2|5=7, 4|10=14, 6^5=3, 3^4=7.
Professor Tian declares that each operator O i with its coming number A i+1 may disappear, and the probability that it happens is P i (0<i<=n). 
 

Input
The input contains several test cases. For each test case, there is a integer n (0<n<=200) in the first line.In the second line, there are n+1 integers, stand for {A i}. The next line contains n operators ,stand for {O i}. The forth line contains {P i}. 
A i will be less than 2 20, 0<=P i<=1.
 

Output
For each text case, you should print the number of text case in the first line.Then output the excepted value of the expression, round to 6 decimal places.
 

Sample Input
   
   
   
   
2 1 2 3 ^ ^ 0.1 0.2 2 8 9 10 ^ ^ 0.5 0.78 1 1 2 & 0.5
 

Sample Output
   
   
   
   
Case 1: 0.720000 Case 2: 4.940000 Case 3: 0.500000
 

Source
2013 Multi-University Training Contest 5
 


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int a[220],A,n;
char op[220][2];
double dp[2][2][22],p[220];

// dp[当前or上一次][是0or是1][是哪一位的]=概率 

int main()
{
    int cas=1;
    while(scanf("%d",&n)!=EOF) 
    {
        scanf("%d",&A);
        for(int i=0;i<n;i++) scanf("%d",a+i);
        for(int i=0;i<n;i++) scanf("%s",op[i]); 
        for(int i=0;i<n;i++) scanf("%lf",p+i);

        memset(dp,0,sizeof(dp));
        int cur=0;

        for(int i=0;i<20;i++) // 20位每一位都判断
        {
            int TB=(A&(1<<i))==0?0:1;
            cur=0;
            dp[cur][TB][i]=1.0;
            for(int j=0;j<n;j++)
            {
                int nxt=1-cur;
                dp[nxt][0][i]=dp[nxt][1][i]=0.0;
                int JB=a[j]&(1<<i);
                if(op[j][0]=='^')
                {
                    if(JB)
                    {
                        dp[nxt][1][i]+=dp[cur][0][i]*(1-p[j]);
                        dp[nxt][0][i]+=dp[cur][1][i]*(1-p[j]);
                    }
                    else if(JB==0)
                    {
                        dp[nxt][1][i]+=dp[cur][1][i]*(1-p[j]);
                        dp[nxt][0][i]+=dp[cur][0][i]*(1-p[j]);
                    }
                }
                else if(op[j][0]=='|')
                {
                    if(JB)     
                    {
                        dp[nxt][1][i]+=dp[cur][0][i]*(1-p[j]);
                        dp[nxt][1][i]+=dp[cur][1][i]*(1-p[j]);
                    }
                    else if(JB==0)
                    {
                        dp[nxt][0][i]+=dp[cur][0][i]*(1-p[j]);
                        dp[nxt][1][i]+=dp[cur][1][i]*(1-p[j]);
                    }
                }
                else if(op[j][0]=='&')
                {
                    if(JB) 
                    {
                        dp[nxt][1][i]+=dp[cur][1][i]*(1-p[j]);
                        dp[nxt][0][i]+=dp[cur][0][i]*(1-p[j]);
                    }
                    else if(JB==0)
                    {
                        dp[nxt][0][i]+=dp[cur][1][i]*(1-p[j]);
                        dp[nxt][0][i]+=dp[cur][0][i]*(1-p[j]);
                    }
                }

                dp[nxt][1][i]+=dp[cur][1][i]*p[j]; 
                dp[nxt][0][i]+=dp[cur][0][i]*p[j]; 
                cur=nxt;
            }
        }
        double ans=0.0,e=1.0;
        int last=n%2;
        for(int i=0;i<20;i++)
        {
            ans+=e*dp[last][1][i];
            e*=2;
        }
        printf("Case %d:\n%.6lf\n",cas++,ans);
    }
    return 0;
}




你可能感兴趣的:(HDOJ 4649 Professor Tian)