玲珑杯 Round 19 1152 - Expected value of the expression (dp)

1152 - Expected value of the expressio



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.




扫描二维码关注公众号,回复: 9905886 查看本文章

input:

The first line contains only one integer n(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).

out:

Output the excepted value of the expression, round to 6 decimal places.

SAMPLE INPUT
21 2 3^ &0.1 0.2
SAMPLE OUTPUT
2.800000



将20位分别开进行考虑计算,dp[i][j] i代表前第几个数运算,在j情况下的概率(j只有两种情况)

#include<stdio.h>
#include<string.h>
#include<math.h>
#define LL long long
const int MAX=1005;
int Ai[MAX];
double p[MAX];
int a[MAX];
char op[MAX][3];
double dp[MAX][3];
int main()
{
 int n;
 int i,j;
 while(scanf("%d",&n)!=EOF)
 {
  for(int i=0;i<=n;i++)scanf("%d",&Ai[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(i=0;i<=20;i++)
  {
   for(int j=0;j<=n;j++)
            {
                if((Ai[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("%.6lf\n",output);
 }
 return 0;
}
 


发布了46 篇原创文章 · 获赞 43 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/wangxiaai/article/details/76358410