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. Python基础灬补充(循环、格式化输出)

    for循环&格式化输出 chinese_zodiac = '鼠牛虎兔龙蛇马羊猴鸡狗猪' for year in range(2000, 2013): print("%s年的生肖是:% ...

  2. 笔试题:C++打印队列

    题目:打印队列 题目介绍:现在用打印机打印队列,已知打印任务有9个优先级(1-9),现在给出一系列任务,求输出打印顺序(任务下标,从0开始). 例: 输入:9,3,5,4,7,1 输出:0,4,2,3 ...

  3. JDBC及DBUtils

    1.JDBC2.DBUtils ###01JDBC概念和数据库驱动程序 * A: JDBC概念和数据库驱动程序 * a: JDBC概述 * JDBC(Java Data Base Connectivi ...

  4. 7.hdfs工作流程及机制

    1. hdfs基本工作流程 1. hdfs初始化目录结构 hdfs namenode -format 只是初始化了namenode的工作目录 而datanode的工作目录是在datanode启动后自己 ...

  5. hive 2以上版本启动异常 Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient

    hive2.0以上的版本启动时 抛出 “Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreCli ...

  6. JS数据结构学习之排序

    在看<>这本书中关于排序这一章的时候,我试着用javascript语言来重写里面几个经典的排序方法,包括冒泡排序.快速排序.选择排序.插入排序还有希尔排序. 一.冒泡排序 冒泡排序算是排序 ...

  7. 20181023-10 Alpha阶段第2周/共2周 Scrum立会报告+燃尽图 07

    作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2290 Scrum master:范靖旋 一.小组介绍 组长:王一可 组员: ...

  8. AOP:Spring的xml配置方式

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. 解决Ubuntu16.04下git clone太慢问题

    记录一些博客,省着自己再去找了... ss-qt5安装 生成.pac genpac --pac-proxy "SOCKS5 127.0.0.1:1080" --gfwlist-pr ...

  10. 【第六周】关于beta测试组员评分标准的若干意见

    组名: 新蜂 组长: 武志远 组员: 宫成荣 谢孝淼 杨柳 李峤 项目名称: java俄罗斯方块 评分规则:简单的才是坠吼的,本组不想搞个大新闻,所以奉行极简的评分方式.每一个人交给组长一个排名,假如 ...