Professor Tian

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
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 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
 
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 250
using 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的更多相关文章

  1. HDU 4649 Professor Tian (概率DP)

    Professor Tian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)To ...

  2. HDU 4649 Professor Tian(DP)

    题目链接 暴力水过的,比赛的时候T了两次,优化一下初始化,终于水过了. #include <cstdio> #include <cstring> #include <st ...

  3. hdu 4649 Professor Tian 反状态压缩+概率DP

    思路:反状态压缩——把数据转换成20位的01来进行运算 因为只有20位,而且&,|,^都不会进位,那么一位一位地看,每一位不是0就是1,这样求出每一位是1的概率,再乘以该位的十进制数,累加,就 ...

  4. hdu 4649 Professor Tian 多校联合训练的题

    这题起初没读懂题意,悲剧啊,然后看了题解写完就AC了 题意是给一个N,然后给N+1个整数 接着给N个操作符(只有三种操作  即  或 ,与 ,和异或 |   &  ^ )这样依次把操作符插入整 ...

  5. HDU 4649 Professor Tian(反状态压缩dp,概率)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:点击打开链接 题目大意 初始有一个数字A0, 然后给出A1,A2..An共n个数字,这n个数字每个数字分别有一 ...

  6. HDU 4649 - Professor Tian(2013MUTC5-1007)(概率)

    不知道这题算作什么类型的题目,反正很巧妙,队友小杰想了没一会就搞定了 为了学习这种方法,我也搞了搞,其实思路不难想,位运算嘛,只有0和1,而且该位的运算只影响该位,最多20位,一位一位地计算即可,只需 ...

  7. HDU 4649 Professor Tian(概率DP)题解

    题意:一个表达式,n + 1个数,n个操作,每个操作Oi和数Ai+1对应,给出每个操作Oi和数Ai+1消失的概率,给出最后表达式值得期望.只有| , ^,&三个位操作 思路:显然位操作只对当前 ...

  8. 【hdu 4696】Professor Tian

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=4649 [Description] 给你一个由位运算"与""或&quo ...

  9. HDU-4694 Professor Tian 概率DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4649 题意:给一个位运算的表达式,每个运算符和其后的运算数有一定概率不计算,求最后表达式的期望. 因为 ...

随机推荐

  1. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅶ(延伸:堆排序的实现)

    2.4.5 堆排序 我们可以把任意优先队列变成一种排序方法.将所有元素插入一个查找最小元素的有限队列,然后再重复调用删除最小元素的操作来将他们按顺序删去.用无序数组实现的优先队列这么做相当于进行一次插 ...

  2. Redis 该选择哪种持久化配置

    这个标题或许会让你想起<黑客帝国>里经典的台词,你要选择蓝色药丸,还是红色药丸? Redis 是我们重度使用的一个开源软件,对它的持久化配置做一番相对深入的总结,是值得的.目前它有两种主流 ...

  3. Android应用开发学习之状态栏通知

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 状态栏通知涉及到两个类,一是Notification,它代表一个通知:另一个是NotificationManager ...

  4. Handsontable 新增一行 默认值

    效果图:

  5. ios实现程序切入后台,实现后台任务

    首先,iOS 会再持续切入后台,给我们5秒钟的时间去处理相关数据,5秒后,程序不会再执行任何代码,处于挂起状态. // 项目需求,按下Home切换后台后向服务器传一些数据,废话不多说,直接上代码 /* ...

  6. Apache、Tomcat、JBoss、WebLogic的区别与关系

    Weblogic: 是一个企业级的应用服务器,其中包括j2ee中的各类应用如jsp,servlet,ejb等 Tomcat:   是一个初级的应用服务器,支持sp和servlet,不支持EJB,如需E ...

  7. Java / Android H基于ttp多线程下载的实现

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/26994463 有个朋友须要个多线程如今的样例,就帮忙实现了.在此分享下~ 先说下 ...

  8. html_day3

    总结学习html的第一天 表格的结构说明 <table></table> <tr></tr> <td></td> <th& ...

  9. photoshop动作面板批量处理图片边框技巧

    1,想给图片加上边框,在不改变图片大小的前提下,可以这样做:ctrl+a,全选图片,然后“编辑”-----“描边”,在跳出来的选项卡里面可以设置边框颜色,大小,位置,及混合模式, ,我们设置好了,就可 ...

  10. [canvas]通过动态生成像素点做绚丽效果

    本例中的粒子就是实实在在的像素,由js代码在canvas上动态生成的像素点!这些像素点通过一个运动方法有规律地动了起来.透过这个思路,我们可以想到很多很炫的效果,但是这个性能有待考察.实验证明,动态控 ...