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                  

 

转载于:https://www.cnblogs.com/GO-NO-1/articles/3377240.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值