hdu4649 概率dp

来源:互联网 发布:西医综合网络课程 编辑:程序博客网 时间:2024/06/16 21:13

http://acm.hdu.edu.cn/showproblem.php?pid=4649

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 Oi with its coming number Ai+1 may disappear, and the probability that it happens is Pi (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 {Ai}. The next line contains n operators ,stand for {Oi}. The forth line contains {Pi}. 
Ai will be less than 220, 0<=Pi<=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
21 2 3^ ^0.1 0.228 9 10^ ^0.5 0.7811 2&0.5
 

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

/**hdu4649 概率dp题目大意:给定一个位运算服连接的表达式,第i个运算符与其后面的数字共同出现和消失,其消失的概率的为pi,问该表达式最后的期望值为多少解题思路:由于为运算没有进位的影响,用dp[i][j][0~1]表示前j个数第i位0或1的期望为多少,那么最后dp[i][n][1]*(1<<i)加入最终结果就好了,           状态转移方程详见代码*/#include <string.h>#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;int n,a[1003];double dp[25][500][2],p[500];char s[1003][2];int main(){    int tt=0;    while(~scanf("%d",&n))    {        for(int i=0; i<=n; i++)        {            scanf("%d",&a[i]);        }        for(int i=1; i<=n; i++)        {            scanf("%s",s[i]);        }        for(int i=1; i<=n; i++)        {            scanf("%lf",&p[i]);        }        double ans=0;        memset(dp,0,sizeof(dp));        for(int i=0; i<=20; i++)        {            if(a[0]&(1<<i))dp[i][0][1]=1;            else dp[i][0][0]=1;            for(int j=1; j<=n; j++)            {                ///第j个符号以及其后面的数字不考虑                dp[i][j][0]+=dp[i][j-1][0]*p[j];                dp[i][j][1]+=dp[i][j-1][1]*p[j];                ///第j个符号以及其后面的数字不考虑                if(a[j]&(1<<i))///第j个数的第i位为1                {                    if(s[j][0]=='&')                    {                        dp[i][j][1]+=dp[i][j-1][1]*(1-p[j]);                        dp[i][j][0]+=dp[i][j-1][0]*(1-p[j]);                    }                    else if(s[j][0]=='|')                    {                        dp[i][j][1]+=dp[i][j-1][0]*(1-p[j]);                        dp[i][j][1]+=dp[i][j-1][1]*(1-p[j]);                    }                    else                    {                        dp[i][j][1]+=dp[i][j-1][0]*(1-p[j]);                        dp[i][j][0]+=dp[i][j-1][1]*(1-p[j]);                    }                }                else///第j个数的第i位为0                {                    if(s[j][0]=='&')                    {                        dp[i][j][0]+=dp[i][j-1][1]*(1-p[j]);                        dp[i][j][0]+=dp[i][j-1][0]*(1-p[j]);                    }                    else if(s[j][0]=='|')                    {                        dp[i][j][0]+=dp[i][j-1][0]*(1-p[j]);                        dp[i][j][1]+=dp[i][j-1][1]*(1-p[j]);                    }                    else                    {                        dp[i][j][0]+=dp[i][j-1][0]*(1-p[j]);                        dp[i][j][1]+=dp[i][j-1][1]*(1-p[j]);                    }                }            }            ans+=(1<<i)*dp[i][n][1];        }        printf("Case %d:\n%.6lf\n",++tt,ans);    }    return 0;}


0 0
原创粉丝点击