“玲珑杯”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: 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 integern(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 = 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
- “玲珑杯”ACM比赛 Round #19 E.Expected value of the expression【Dp】
- “玲珑杯”ACM比赛 Round #4 E -- array(dp)
- 玲珑杯”ACM比赛 Round #4 E -- array【DP】
- 玲珑学院OJ 1152-Expected value of the expression(概率DP)
- “玲珑杯”ACM比赛 Round #12【dp】
- “玲珑杯”ACM比赛 Round #19
- “玲珑杯”ACM比赛 Round #4 E题
- “玲珑杯”ACM比赛 Round #22 E【贪心】
- “玲珑杯”ACM比赛 Round #18 C(dp)
- “玲珑杯”ACM比赛 Round #16 Down the Rabbit Hole
- 玲珑杯 1066(“玲珑杯”ACM比赛 Round #6)(区间DP+四边形不等式优化)
- “玲珑杯”ACM比赛 Round #19 B.Buildings【二分+RMQ】
- 玲珑杯”ACM比赛 Round #19 A simple math problem
- “玲珑杯”ACM比赛 Round #4
- 玲珑杯”ACM比赛 Round #5
- “玲珑杯”ACM比赛 Round #7
- “玲珑杯”ACM比赛 Round #7
- “玲珑杯”ACM比赛 Round #11 " ---1097
- CodeChef ISCC2017 HISTOSIM
- hdu4607—Park Visit(树的直径)
- Codis源码解析——proxy添加到集群
- 深入浅出Tensorflow(五):循环神经网络简介
- NKOJ 4040 (CQOI 2017) 小Q的表格(莫比乌斯反演+分块+递推+线性筛/欧拉函数+分块+线性筛)
- “玲珑杯”ACM比赛 Round #19 E.Expected value of the expression【Dp】
- 老生常谈之SQL Server (行转列,列转行)
- 环境变量的配置
- golang 实现一种环形队列,及周期任务
- 打造 高性能,轻量级的 webform框架---js直接调后台
- 设计模式基本原则
- 结合Unity浅谈设计模式-单例
- MyBatis中Like语句使用总结
- 如何通过配置文件链接数据库?