1152 - Expected value of the expression

Time Limit:2s Memory Limit:128MByte

Submissions:128Solved:63

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
SOLUTION
题意:给你n+1个数,n个位运算符,第一个数到第n个数和对应的n个运算符一起消失的概率为p[i],问你运算结果的期望。
题解:dp[i][j][k]   前i个数 a[i]二进制第j位置填k的概率 具体看代码中的转移方程
 #pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
#define ll long long
#define mod 1000000007
using namespace std;
int n;
char s[];
int a[];
char o[];
double p[];
double dp[][][];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
getchar();
gets(s);
int len=strlen(s);
int res=;
for(int i=;i<len;i++){
if(s[i]!=' '){
o[res++]=s[i];
}
}
for(int i=;i<=n;i++){
scanf("%lf",&p[i]);
}
int now;
for(int i=;i<=;i++){//初始化
now=(a[]>>(i-));
if(now%==){
dp[][i][]=1.0;
dp[][i][]=0.0;
}
else{
dp[][i][]=0.0;
dp[][i][]=1.0;
}
}
for(int i=;i<=n;i++){
for(int j=;j<=;j++){
dp[i][j][]+=dp[i-][j][]*p[i];//消失
dp[i][j][]+=dp[i-][j][]*p[i];
}
if(o[i]=='^'){
for(int j=;j<=;j++){//不消失
now=(a[i]>>(j-));
if(now%==){
dp[i][j][]+=dp[i-][j][]*(1.0-p[i]);
dp[i][j][]+=dp[i-][j][]*(1.0-p[i]);
}
else
{
dp[i][j][]+=dp[i-][j][]*(1.0-p[i]);
dp[i][j][]+=dp[i-][j][]*(1.0-p[i]);
}
}
}
if(o[i]=='|'){
for(int j=;j<=;j++){
now=(a[i]>>(j-));
if(now%==){
dp[i][j][]+=(dp[i-][j][]+dp[i-][j][])*(1.0-p[i]); }
else
{
dp[i][j][]+=dp[i-][j][]*(1.0-p[i]);
dp[i][j][]+=dp[i-][j][]*(1.0-p[i]);
}
} }
if(o[i]=='&'){
for(int j=;j<=;j++){
now=(a[i]>>(j-));
if(now%==){
dp[i][j][]+=dp[i-][j][]*(1.0-p[i]);
dp[i][j][]+=dp[i-][j][]*(1.0-p[i]);
}
else
{
dp[i][j][]+=(dp[i-][j][]+dp[i-][j][])*(1.0-p[i]);
}
}
}
}
double ans=;
now=;
for(int i=;i<=;i++){
ans=ans+(dp[n][i][])*now;
now*=;
}
printf("%.6f\n",ans);
return ;
}

玲珑学院oj 1152 概率dp的更多相关文章

  1. 玲珑学院OJ 1028 - Bob and Alice are playing numbers 字典树,dp

    http://www.ifrog.cc/acm/problem/1028 题解处:http://www.ifrog.cc/acm/solution/4 #include <cstdio> ...

  2. Light oj 1030 概率DP

    D - Discovering Gold Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768 ...

  3. 玲珑学院OJ 1023 - Magic boy Bi Luo with his excited math problem 树状数组暴力

    分析:a^b+2(a&b)=a+b  so->a^(-b)+2(a&(-b))=a-b 然后树状数组分类讨论即可 链接:http://www.ifrog.cc/acm/probl ...

  4. 概率DP light oj 1030

    t组数据 n块黄金 到这里就捡起来 出发点1 到n结束  点+位置>n 重掷一次 dp[i] 代表到这里的概率 dp[i]=(dp[i-1]+dp[i-2]... )/6  如果满6个的话 否则 ...

  5. Light OJ 1317 Throwing Balls into the Baskets 概率DP

    n个人 m个篮子 每一轮每一个人能够选m个篮子中一个扔球 扔中的概率都是p 求k轮后全部篮子里面球数量的期望值 依据全期望公式 进行一轮球数量的期望值为dp[1]*1+dp[2]*2+...+dp[ ...

  6. 动态规划——概率dp

    所谓概率dp,用动态规划的思想找到一个事件中可能发生的所有情况,然后找到符合要求的那些情况数,除以总数便可以得到符合要求的事件发生的概率.其核心思想还是通过dp来得到事件发生的所有情况,很类似在背包专 ...

  7. A Dangerous Maze (II) LightOJ - 1395(概率dp)

    A Dangerous Maze (II) LightOJ - 1395(概率dp) 这题是Light Oj 1027的加强版,1027那道是无记忆的. 题意: 有n扇门,每次你可以选择其中一扇.xi ...

  8. Codeforces 28C [概率DP]

    /* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...

  9. HDU 4405 Aeroplane chess (概率DP)

    题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i  这个位置到达 n ...

随机推荐

  1. mac安装pkg 一直“正在验证” 卡着

    今天换了新mac, 但是之前wireshark(抓包工具) 不能用了 ,要安装Xquartz. 下载之后一直卡着, 网上找了半天没有解决方法. 最后我重启一下就好了... 重启一下. 2. 15款ma ...

  2. 记因内核版本错误导致U盘不能识别的问题解决

    U盘插上电脑,发现没有自动挂载.然后运行sudo fdisk -l一看,发现并没有U盘所对应的设备,也就是U盘不能识别了!以前从没在Linux上遇到这种问题,通过查资料得知,要识别U盘,需要装载usb ...

  3. gopherjs

    An example implementation of a GopherJS client and a Go server using the Improbable gRPC-Web impleme ...

  4. leetcode个人题解——#33 Search in Rotated Sorted Array

    思路:每次取中间元素,一定有一半有序,另一半部分有序,有序的部分进行二分查找,部分有序的部分递归继续处理. class Solution { public: ; int middleSearch(in ...

  5. python3【基础】-and和or的短路逻辑

    1. 表达式只有一个逻辑运算符 python中哪些对象会被当成False,哪些又是True呢? 基本数据类型中的None.任何数值类型中的0.空字符串"",空列表[],空元组()和 ...

  6. ES6的新特性(3)——变量的解构赋值

    变量的解构赋值 数组的解构赋值 基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring). let a = 1; let b = 2; le ...

  7. 冲刺ing-2

    第二次Scrum冲刺 队员完成的任务 队员 完成任务 吴伟华 分配任务 蔺皓雯 编写博客,查阅资料 蔡晨旸 查阅资料 曾茜 暂无 鲁婧楠 暂无 杨池宇 暂无 成员遇到的问题 队员 问题 吴伟华 暂无 ...

  8. 软件工程 - 第二十次作业 Alpha 事后诸葛亮(团队)

    Alpha 事后诸葛亮(团队) 组长本次作业链接:https://www.cnblogs.com/dawnduck/p/10056026.html 现代软件工程 项目Postmortem 设想和目标 ...

  9. winform 弹出窗体指定位置

    在启动一个程序时,我们希望窗口显示的位置处于屏幕的正中心,可以如下设置: MainForm mainForm = new MainForm(); mainForm.StartPosition = Fo ...

  10. 1029 C语言文法翻译(2)

    program à external_declaration | program external_declaration 翻译:<源程序>→ <外部声明> | <源程序 ...