Professor Tian

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 870    Accepted Submission(s):
479

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

题目大意

初始有一个数字A0, 然后给出A1,A2..An共n个数字,这n个数字每个数字分别有一个操作符,&,|,^

且每个数字出现的概率是pi

如果某个数字出现了,那么就和前面的数字用它的操作符进行位运算。

问最终的期望值是多少?

思路

这题官方题解说是反状态压缩,还是第一次做这种题。

知道了怎么表示状态这题就觉得不难做了,赛后1A。

题解官方的已经很详细了,不再累赘:

反状态压缩——把数据转换成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<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std; typedef long long int64;
const int INF = 0x3f3f3f3f;
const int MAXN = ; int n, m;
double f[MAXN][];
int A[MAXN];
char O[MAXN];
double p[MAXN]; int main(){ char op[];
int cas = ;
while(~scanf("%d", &n)){ for(int i=; i<=n; ++i)
scanf("%d", &A[i]); for(int i=; i<=n; ++i){
scanf("%s", op);
O[i] = op[];
} for(int i=; i<=n; ++i)
scanf("%lf", &p[i]); for(int i=; i<; ++i)
f[][] = (A[i]>>i)&; double ans = ;
for(int i=; i<; ++i){
f[][] = (A[]>>i)&;
f[][] = !f[][]; for(int j=; j<=n; ++j){ // 第j个数字不出现,0和1的概率
f[j][] = f[j-][] * p[j];
f[j][] = f[j-][] * p[j]; // 加上出现第j个数字,0和1的概率
if(O[j] == '^'){
if( (A[j]>>i) & ){
f[j][] += f[j-][]*(-p[j]);
f[j][] += f[j-][]*(-p[j]);
} else{
f[j][] += f[j-][]*(-p[j]);
f[j][] += f[j-][]*(-p[j]);
} } else if(O[j] == '&'){
if( (A[j]>>i) & ){
f[j][] += f[j-][]*(-p[j]);
f[j][] += f[j-][]*(-p[j]);
} else{
f[j][] += (f[j-][] + f[j-][]) * (-p[j]);
// f[j][1]: 如果用了第j个数,这里不能出现1
}
} else if(O[j] == '|'){
if( (A[j]>>i) & ){
// f[j][0]: 不能出现这种情况
f[j][] += (f[j-][] + f[j-][]) * (-p[j]);
} else{
f[j][] += f[j-][] * (-p[j]);
f[j][] += f[j-][] * (-p[j]);
}
}
}
ans = ans + f[n][] * (<<i);
} printf("Case %d:\n%.6f\n", cas++, ans); }
return ;
}

HDU 4649 Professor Tian (概率DP)的更多相关文章

  1. HDU 4649 Professor Tian

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

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

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

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

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

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

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

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

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

  6. HDU 4649 Professor Tian(DP)

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

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

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

  8. HDU-4694 Professor Tian 概率DP

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

  9. HDU 4089 Activation(概率DP)(转)

    11年北京现场赛的题目.概率DP. 公式化简起来比较困难....而且就算结果做出来了,没有考虑特殊情况照样会WA到死的.... 去参加区域赛一定要考虑到各种情况.   像概率dp,公式推出来就很容易写 ...

随机推荐

  1. JZOJ 5913. 林下风气

    Description 里口福因有林下风气,带领全国各地高校掀起了一股AK风,大家都十分痴迷于AK.里口福为了打击大家的自信心,出了一道自以为十分困难的题目.里口福有一棵树,第i个节点上有点权ai,他 ...

  2. random模块 time模块的用法 python

    1.random()模块的使用 import random x = random.random() y = random.random() print(x,y*10) #random.random() ...

  3. POJ:2139-Six Degrees of Cowvin Bacon

    传送门:http://poj.org/problem?id=2139 Six Degrees of Cowvin Bacon Time Limit: 1000MS Memory Limit: 6553 ...

  4. SVD在推荐系统中的应用详解以及算法推导

    SVD在推荐系统中的应用详解以及算法推导     出处http://blog.csdn.net/zhongkejingwang/article/details/43083603 前面文章SVD原理及推 ...

  5. 10,python开发之virtualenv与virtualenvwrapper

      在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程 ...

  6. python-openpyxl操作excel

    python 读写 excel有很多选择,但是,方便操作的库不多,在我尝试了几个库之后,我觉得两个比较方便的库是xlrd/xlwt.openpyxl. 之所以推荐这两个库是因为这两个库分别操作的是不同 ...

  7. POJ 3580 SuperMemo 伸展树

    题意: 维护一个序列,支持如下几种操作: ADD x y D:将区间\([x,y]\)的数加上\(D\) REVERSE x y:翻转区间\([x,y]\) REVOLVE x y T:将区间\([x ...

  8. laravel5.5事件系统

    目录 1 注册事件和监听器 2 定义事件 3 定义监听器 4 分发事件 更多使用方法 1. 可以手动注册事件 2. 事件监听器中调用队列 3.事件订阅者 1 注册事件和监听器 1.修改EventSer ...

  9. +(void)load; +(void)initialize;有什么用处?

    总得来说: 1.+load方法是在main函数之前调用的: 2.遵从先父类后子类,先本类后列类别的顺序调用: 3.类,父类与分类之间的调用是互不影响的.子类中不需要调用super方法,也不会调用父类的 ...

  10. Mac 小技巧

    本文的大部分技巧来自于池建强老师的<MacTalk.人生元编程>,感谢他的辛苦付出,本文多系整理而已. 终端输入说英语 说英语时我们当然希望有标准发音.在Mac中不需要字典,直接在终端里输 ...