hdu 4649 Professor Tian(期望)

Professor Tian

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


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

题意:给出一个二进制点的位运算,和每个位运算数字和符号出现的概率,求答案的期望;

解:本质上就是求一个二进制数的每一位的是1的概率对答案的贡献,为0的不考虑



#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include <bits/stdc++.h>
using namespace std;
const int N = 1100000+10;
typedef long long LL;
const LL mod = 1e9+7;
char str[N][3];
int a[N];
double p[N];

int main()
{
    int ncase=1, n;
    while(scanf("%d", &n)!=EOF)
    {
        for(int i=0;i<=n;i++) scanf("%d", &a[i]);
        for(int i=1;i<=n;i++) scanf("%s",str[i]);
        for(int i=1;i<=n;i++) scanf("%lf", &p[i]);
        double ans=0, q;
        for(int i=0;i<=20;i++)
        {
            if(a[0]&(1<<i)) q=1;
            else q=0;
            for(int j=1;j<=n;j++)
            {
                if(a[j]&(1<<i))
                {
                    if(str[j][0]=='^') q=q*p[j]+(1-q)*(1-p[j]);
                    else if(str[j][0]=='|') q=q*p[j]+(1-p[j]);
                }
                else
                {
                    if(str[j][0]=='&') q=q*p[j];
                }
            }
            ans+=(1<<i)*q;
        }
        printf("Case %d:\n",ncase++);
        printf("%.6f\n",ans);
    }
    return 0;
}








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值