“玲珑杯”ACM比赛 Round #19 E.Expected value of the expression【Dp】

来源:互联网 发布:拇指特效软件 编辑:程序博客网 时间:2024/06/06 01:27

1152 - Expected value of the expression

Time Limit:2s Memory Limit:128MByte

Submissions:82Solved:45

DESCRIPTION

You are given an expression: A0O1A1O2A2OnAnA0O1A1O2A2⋯OnAn, where Ai(0in)Ai(0≤i≤n) represents number, Oi(1in)Oi(1≤i≤n) represents operator. There are three operators, &,|,^&,|,^, which means and,or,xorand,or,xor, and they have the same priority.

The ii-th operator OiOi and the numbers AiAi disappear with the probability of pipi.

Find the expected value of an expression.

INPUT
The first line contains only one integern(1n1000)n(1≤n≤1000).The second line contains n+1n+1 integers Ai(0Ai<220)Ai(0≤Ai<220).The third line contains nn chars OiOi.The fourth line contains nn floats pi(0pi1)pi(0≤pi≤1).
OUTPUT
Output the excepted value of the expression, round to 6 decimal places.
SAMPLE INPUT
2
1 2 3
^ &
0.1 0.2
SAMPLE OUTPUT
2.800000
HINT
Probability = 0.1 * 0.2 Value = 1Probability = 0.1 * 0.8 Value = 1 & 3 = 1Probability = 0.9 * 0.2 Value = 1 ^ 2 = 3Probability = 0.9 * 0.8 Value = 1 ^ 2 & 3 = 3Expected Value = 0.1 * 0.2 * 1 + 0.1 * 0.8 * 1 + 0.9 * 0.2 * 3 + 0.9 * 0.8 * 3 = 2.80000
SOLUTION
“玲珑杯”ACM比赛 Round #19

题目大意:

有N+1个数Ai.有N个位运算操作,在其中间隔排列,当前位运算操作出现的概率是1-pi,问最终获得的值的期望。


思路:


按位dp.分成20位、设定Dp【i】【2】:

①Dp【i】【0】表示对应当前位,到了第i个操作符,运算结果为0的概率

②Dp【i】【1】表示对应当前位,到了第i个操作符,运算结果为1的概率


那么状态转移方程我们到当前位i分两种情况考虑即可:

①当前操作符不粗线了。

②当前操作符出现。


然后再分操作符的三种情况转移一下就行了。


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;int a[15000];int num[15000];char op[15000][3];double p[15000];double dp[15000][3];int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=0;i<=n;i++)scanf("%d",&num[i]);        for(int i=1;i<=n;i++)scanf("%s",op[i]);        for(int i=1;i<=n;i++)scanf("%lf",&p[i]);        double output=0;        for(int i=0;i<=20;i++)        {            for(int j=0;j<=n;j++)            {                if((num[j]&(1<<i))>0)                {                    a[j]=1;                }                else a[j]=0;            }            memset(dp,0,sizeof(dp));            dp[0][a[0]]=1;            for(int j=1;j<=n;j++)            {                dp[j][0]=dp[j-1][0]*p[j];                dp[j][1]=dp[j-1][1]*p[j];                if(op[j][0]=='|')                {                    if(a[j]==0)                    {                        dp[j][0]+=dp[j-1][0]*(1-p[j]);                        dp[j][1]+=dp[j-1][1]*(1-p[j]);                    }                    if(a[j]==1)                    {                        dp[j][1]+=dp[j-1][0]*(1-p[j]);                        dp[j][1]+=dp[j-1][1]*(1-p[j]);                    }                }                if(op[j][0]=='&')                {                    if(a[j]==0)                    {                        dp[j][0]+=dp[j-1][0]*(1-p[j]);                        dp[j][0]+=dp[j-1][1]*(1-p[j]);                    }                    if(a[j]==1)                    {                        dp[j][0]+=dp[j-1][0]*(1-p[j]);                        dp[j][1]+=dp[j-1][1]*(1-p[j]);                    }                }                if(op[j][0]=='^')                {                    if(a[j]==0)                    {                        dp[j][0]+=dp[j-1][0]*(1-p[j]);                        dp[j][1]+=dp[j-1][1]*(1-p[j]);                    }                    if(a[j]==1)                    {                        dp[j][0]+=dp[j-1][1]*(1-p[j]);                        dp[j][1]+=dp[j-1][0]*(1-p[j]);                    }                }            }            output+=(dp[n][1]*(1<<i));        }        printf("%.6f\n",output);    }}












阅读全文
0 0