HDU 4649 Professor Tian
来源:互联网 发布:运动软件方案 编辑:程序博客网 时间:2024/06/05 09:30
Professor Tian
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 376 Accepted Submission(s): 199
Total Submission(s): 376 Accepted Submission(s): 199
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).
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.
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
Source
2013 Multi-University Training Contest 5
Recommend
zhuyuanchen520
贴一下解题报告的思路:
贴一下解题报告的思路:
反状态压缩——把数据转换成20位的01来进行运算
因为只有20位,而且&,|,^都不会进位,那么一位一位地看,每一位不是0就是1,这样求出每一位是1的概率,再乘以该位的十进制数,累加,就得到了总体的期望。
对于每一位,状态转移方程如下:
f[i][j]表示该位取前i个数,运算得到j(0或1)的概率是多少。
f[i][1]=f[i-1][1]*p[i]+根据不同运算符和第i位的值运算得到1的概率。
f[i][0]同理。
初始状态:f[0][0~1]=0或1(根据第一个数的该位来设置)
每一位为1的期望 f[n][1]
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#define N 250using namespace std;int num[N];double p[N],dp[N][2];char opr[N],s1[N];int main(){ //freopen("data.in","r",stdin); int n,tem=1; char c1,c2; while(scanf("%d",&n)!=EOF) { for(int i=0;i<=n;i++) { scanf("%d",&num[i]); } getchar(); gets(s1); int l = strlen(s1); for(int i=0,j=0;i<=l-1;i++) { if(s1[i]=='|'||s1[i]=='^'||s1[i]=='&') { opr[j] = s1[i]; j++; } } for(int i=0;i<=n-1;i++) { scanf("%lf",&p[i]); } double ans=0.0; for(int i=1;i<=20;i++) { int t=num[0]&1; dp[0][t] = 1; dp[0][(t+1)%2] = 0; num[0]=(num[0]>>1); for(int j=1;j<=n;j++) { int k = num[j]&1; num[j]=(num[j]>>1); char c = opr[j-1]; if(c=='|') { if(k==1) { dp[j][1] = dp[j-1][1]+dp[j-1][0]*(1-p[j-1]); dp[j][0] = dp[j-1][0]*p[j-1]; }else { dp[j][1] = dp[j-1][1]; dp[j][0] = dp[j-1][0]; } continue; }else if(c=='&') { if(k==1) { dp[j][1] = dp[j-1][1]; dp[j][0] = dp[j-1][0]; }else { dp[j][1] = dp[j-1][1]*p[j-1]; dp[j][0] = dp[j-1][1]*(1-p[j-1])+dp[j-1][0]; } continue; }else if(c=='^') { if(k==1) { dp[j][1] = dp[j-1][0]*(1-p[j-1])+dp[j-1][1]*p[j-1]; dp[j][0] = dp[j-1][1]*(1-p[j-1])+dp[j-1][0]*p[j-1]; }else { dp[j][1] = dp[j-1][1]; dp[j][0] = dp[j-1][0]; } continue; } } ans += (1<<(i-1))*dp[n][1]; } printf("Case %d:\n",tem++); printf("%.6lf\n",ans); } return 0;}
- hdu 4649 Professor Tian
- hdu 4649 Professor Tian
- HDU 4649 Professor Tian
- HDU 4649 Professor Tian
- hdu 4649 Professor Tian
- hdu 4649 Professor Tian (DP)
- DP hdu 4649 Professor Tian
- hdu 4649 Professor Tian(期望)
- hdu 4649——Professor Tian
- hdu - 4649 - Professor Tian(概率dp)
- HDU 4649 - Professor Tian(概率DP)
- HDU 4649 Professor Tian (概率DP)
- HDU-4649 Professor Tian(期望dp)
- HDU 4649 Professor Tian(概率DP)
- 【HDU】4649 Professor Tian 期望DP
- HDOJ 4649 Professor Tian
- HDU 4649 Professor Tian (2013多校联合5 1007)
- hdu 4649 Professor Tian 多校第五场
- linux内核编程以及播放工具
- FTP命令及参数详解
- 开源项目SlidingMenu的使用
- Oracle 创建dblink
- Python学习笔记--为什么需要使用__name__=='__main__'
- HDU 4649 Professor Tian
- Linux下ISE开发环境的安装
- 牛人的博客地址,多看看
- MyEclipse插件介绍与下载
- 分布式开发(webservice)
- 开源项目SlidingMenu的示例
- 经常忽略错误之where 和 and
- 频度与贝叶斯――白猫黑猫?
- QT中QPainterPath类的功能和使用方法