dp好题 玲珑杯 Expected value of the expression
152 - Expected value of the expression
Time Limit:2s Memory Limit:128MByte
Submissions:135Solved:65
DESCRIPTION
You are given an expression: A0O1A1O2A2⋯OnAnA0O1A1O2A2⋯OnAn, where Ai(0≤i≤n)Ai(0≤i≤n) represents number, Oi(1≤i≤n)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 integer n(1≤n≤1000)n(1≤n≤1000). The second line contains n+1n+1 integers Ai(0≤Ai<220)Ai(0≤Ai<220). The third line contains nn chars OiOi. The fourth line contains nn floats pi(0≤pi≤1)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 = 1 Probability = 0.1 * 0.8 Value = 1 & 3 = 1
Probability = 0.9 * 0.2 Value = 1 ^ 2 = 3
Probability = 0.9 * 0.8 Value = 1 ^ 2 & 3 = 3
Expected Value = 0.1 * 0.2 * 1 + 0.1 * 0.8 * 1 + 0.9 * 0.2 * 3 + 0.9 * 0.8 * 3 = 2.80000
疏于补题,在这种学习环境下我也很难进步,要想做的足够优秀,本来就是要花时间和别人不一样啊
想下前两天有人讲状压dp,讲完那道题我应该会做了,因为我做过状压dp的题,但是换一道一定是不一定的啊,但是我觉得作为一个讲解者,我会倾向于向大家普及更多的知识,从而共同进步
闲话还是少说好了,等觉得真的有些难受的话就写些鸡汤,鼓励下正在努力的自己
概率dp,往常也和期望有关,什么是期望呢,饿了么霸王餐30个人付30元抽到20元的红包,我投资1元红包中奖期望是0.67元,这明摆着就是亏得生意啊,但是赌徒心理会致使我买下去,其实中奖了并不意味着省钱,往常中奖了就有会去吃顿好的
也就是所得分数*概率,把所有的可能都累城下,这个内容是高中学的,但是我可不可以合并同类项啊
E=(Σp1*(E1+X1)+Σp2*X2)/(1-Σp2)得到这样一个通项公式,这样能在中间就对数据进行处理
给你一个n+1个数进行n个位操作和他们出现的概率,求下这个表达式的期望
再结合这个题目的意思和运算符的一些问题,进行状态转移就好了
#include<bits/stdc++.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 ans=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]=='|') { dp[j][0^a[j]]+=dp[j-1][0]*(1-p[j]); dp[j][1]+=dp[j-1][1]*(1-p[j]); } else if(op[j][0]=='&') { dp[j][0]+=dp[j-1][0]*(1-p[j]); dp[j][0^a[j]]+=dp[j-1][1]*(1-p[j]); } else { dp[j][0]+=dp[j-1][0^a[j]]*(1-p[j]); dp[j][1]+=dp[j-1][a[j]^1]*(1-p[j]); } } ans+=(dp[n][1]*(1<<i)); } printf("%.6f\n",ans); } return 0; }
本文来自博客园,作者:暴力都不会的蒟蒻,转载请注明原文链接:https://www.cnblogs.com/BobHuang/p/7325120.html