HDU 4649 Professor Tian (概率DP)

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

2013
Multi-University Training Contest 5

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 = 210;

int n, m;
double f[MAXN][2];
int A[MAXN];
char O[MAXN];
double p[MAXN];

int main(){

    char op[10];
    int cas = 1;
    while(~scanf("%d", &n)){

        for(int i=0; i<=n; ++i)
            scanf("%d", &A[i]);

        for(int i=1; i<=n; ++i){
            scanf("%s", op);
            O[i] = op[0];
        }

        for(int i=1; i<=n; ++i)
            scanf("%lf", &p[i]); 

        for(int i=0; i<20; ++i)
            f[0][0] = (A[i]>>i)&1;

        double ans = 0;
        for(int i=0; i<20; ++i){
            f[0][1] = (A[0]>>i)&1;
            f[0][0] = !f[0][1];

            for(int j=1; j<=n; ++j){

                // 第j个数字不出现,0和1的概率
                f[j][0] = f[j-1][0] * p[j];
                f[j][1] = f[j-1][1] * p[j];

                // 加上出现第j个数字,0和1的概率
                if(O[j] == ‘^‘){
                    if( (A[j]>>i) & 1 ){
                        f[j][0] += f[j-1][1]*(1-p[j]);
                        f[j][1] += f[j-1][0]*(1-p[j]);
                    } else{
                        f[j][0] += f[j-1][0]*(1-p[j]);
                        f[j][1] += f[j-1][1]*(1-p[j]);
                    }

                } else if(O[j] == ‘&‘){
                    if( (A[j]>>i) & 1 ){
                        f[j][0] += f[j-1][0]*(1-p[j]);
                        f[j][1] += f[j-1][1]*(1-p[j]);
                    } else{
                        f[j][0] += (f[j-1][0] + f[j-1][1]) * (1-p[j]);
                       // f[j][1]: 如果用了第j个数,这里不能出现1
                    }
                } else if(O[j] == ‘|‘){
                    if( (A[j]>>i) & 1){
                       // f[j][0]: 不能出现这种情况
                        f[j][1] += (f[j-1][0] + f[j-1][1]) * (1-p[j]);
                    } else{
                        f[j][0] += f[j-1][0] * (1-p[j]);
                        f[j][1] += f[j-1][1] * (1-p[j]);
                    }
                }
            }
            ans = ans + f[n][1] * (1<<i);
        }

        printf("Case %d:\n%.6f\n", cas++, ans);

    }
    return 0;
}
时间: 2024-10-05 06:50:27

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

HDU 4649 Professor Tian(概率DP)题解

题意:一个表达式,n + 1个数,n个操作,每个操作Oi和数Ai+1对应,给出每个操作Oi和数Ai+1消失的概率,给出最后表达式值得期望.只有| , ^,&三个位操作 思路:显然位操作只对当前位相关,那么我们可以一位一位求解,算出每一位的概率,然后算出这一位所给出的贡献的期望. 代码: #include<set> #include<map> #include<cmath> #include<queue> #include<cstdio>

HDU 4652 Dice (概率DP)

Dice Problem Description You have a dice with m faces, each face contains a distinct number. We assume when we tossing the dice, each face will occur randomly and uniformly. Now you have T query to answer, each query has one of the following form: 0

HDU 3366 Passage (概率DP)

Passage Problem Description Bill is a millionaire. But unfortunately he was trapped in a castle. There are only n passages to go out. For any passage i (1<=i<=n), Pi (0<=Pi<=1) denotes the probability that Bill will escape from this castle saf

[ACM] HDU 4576 Robot (概率DP,滚动数组)

Robot Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells. The cells are numbered from 1 to n clockwise. At first the robot is in cell 1. Then Michael uses a remote control to send m commands to the ro

[ACM] hdu 4405 Aeroplane chess (概率DP)

Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the number

HDU 4050 wolf5x (概率DP 求期望)

题意:有N个格子,1~N,起点在0,每个格子有一个状态(0,1,2,3),每次可以跨[a,b]步, 问走完N个格子需要步数的期望,每次尽量走小的步数,即尽量走a步,不能则走a+1,-- 状态0意味着你不能踏进对应的网格. 状态1意味着你可以??步入网格用你的左腿. 状态2意味着你可以??步入网格用你的右腿. 状态3意味着你可以进入网格用任何你的腿,而接下来的步骤中,您可以使用任何的腿;即你不需要遵循上述规则. 思路:借鉴了各路大神的思想理解了下. dp[i][j] :表示走到第 i 个格子在 j

[ACM] hdu 3853 LOOPS (概率DP,递推)

LOOPS Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help her friend Madoka save the world. But because of the plot of the Boss Incubator, she is trapped in a labyrinth called LOOPS. The planform of the

HDU4649:Professor Tian(概率)

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 proble

HDU 4336 Card Collector(概率DP)

 题意:有n种卡片,吃零食的时候会吃到一些卡片,告诉你在一袋零食中吃到每种卡片的概率,求搜集齐每种卡片所需要买零食的袋数的期望. 思路:先状态压缩,然后概率DP 用d[i]表示由状态i到目标需要再买多少包,则状态转移方程为d[i] = p'*(d[i]+1) + sigma(d[ i | (1 << j) * p[i] ),然后相同项移到左边,最后就可以得到答案. #include<cstdio> #include<cstring> #include<cmat