hdu 4649 Professor Tian

Professor Tian
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 632 Accepted Submission(s): 346


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
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


Recommend
zhuyuanchen520

 

 1 //15MS    284K    1433 B    C++    
 2 /*
 3 
 4     题意:
 5         初始有一个数字A0, 然后给出A1,A2..An共n个数字,
 6     这n个数字每个数字分别有一个操作符,&,|,^且每个符号
 7     消失的概率是pi,如果某个符号出现了,那么就和前面的数字
 8     用它的操作符进行位运算。
 9     问最终的期望值是多少?
10     
11     反状态压缩:
12         dp[i][j]表示处理到第i个数时第j位出现1的概率 
13     
14     初始化,dp[0][j]=(a[0]>>j)&1(0<=j<=20)。
15     1.第i个符号为&,这时若di为1,则dp[i][j]=dp[i-1][j],
16     否则,dp[i][j]=pi*dp[i-1][j];
17 
18     2.第i个符号为|,这时若di为1,则 dp[i][j]=pi*dp[i-1][j]+1-pi,
19     否则,dp[i][j]=dp[i-1][j];
20 
21     3.第i个符号位^,这时若di为1,则dp[i][j]=dp[i-1][j]*pi+(1-pi)*(1-dp[i-1][j]);,
22     否则,dp[i][j]=dp[i-1][j];
23     
24     最后的答案: 
25     dp[n][0]*2^0+dp[n][1]*2^1+dp[n][2]*2^2+...
26     ...dp[n][20]*2^20.(这里的^为乘方运算)
27 
28 */
29 #include<stdio.h>
30 #include<string.h>
31 double dp[205][25];
32 int a[205];
33 double p[205];
34 char op[205];
35 int main(void)
36 {
37     int n;
38     int k=1;
39     while(scanf("%d",&n)!=EOF)
40     {
41         memset(dp,0,sizeof(dp));
42         for(int i=0;i<=n;i++)
43             scanf("%d",&a[i]);
44         for(int i=1;i<=n;i++)
45             scanf(" %c",&op[i]);
46         for(int i=1;i<=n;i++)
47             scanf("%lf",&p[i]);
48         for(int i=0;i<=20;i++)
49             dp[0][i]=(double)((a[0]>>i)&1);
50         for(int i=1;i<=n;i++){
51             for(int j=0;j<=20;j++){
52                 int temp=(1&(a[i]>>j));
53                 if(op[i]=='&'){
54                     if(temp)
55                         dp[i][j]=dp[i-1][j];
56                     else dp[i][j]=p[i]*dp[i-1][j];
57                 }
58                 if(op[i]=='|'){
59                     if(temp)
60                         dp[i][j]=p[i]*dp[i-1][j]+1-p[i];
61                     else dp[i][j]=dp[i-1][j];
62                 }
63                 if(op[i]=='^'){
64                     if(temp) 
65                         dp[i][j]=p[i]*dp[i-1][j]+(1-p[i])*(1-dp[i-1][j]);
66                     else dp[i][j]=dp[i-1][j];
67                 }
68             }
69         }
70         double ans=0;
71         double temp=1;
72         for(int i=0;i<=20;i++){
73             ans+=dp[n][i]*temp;
74             temp*=2;
75         }
76         printf("Case %d:\n%lf\n",k++,ans);
77     }
78     return 0;
79 }
80                  

 

posted @ 2013-10-19 08:46  heaventouch  阅读(131)  评论(0编辑  收藏  举报