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. Saving James Bond - Easy Version (MOOC)

    06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...

  2. lxd&openstack-lxd源码剖析

    lxd:https://linuxcontainers.org/lxd/,目标是融入到openstack体系被管理,像虚拟机一样被管理使用.从如下图可知,并非走的是libvirt-lxc路线,而是no ...

  3. sqli-labs学习笔记 DAY1

    DAY 1 准备工作 安装phpstudy 安装配置sqli-labs 学习笔记 SQL语句的注释:–, # +在URL经过编码后会编码为空格 SQL语句的查询语句:SELECT column_nam ...

  4. python项目通过配置文件方式配置日志-logging

    背景:项目中引入日志是必须的,这里介绍通过配置文件config.ini的方式配置日志 1.新建config.ini 2.添加配置 [loggers]keys=root,ProxyIP [handler ...

  5. A Product Recall 产品召回

    Rick: The Board of Directors has come to a decision. Our company will take an image hit, and it's go ...

  6. Python 中的实用数据挖掘

    本文是 2014 年 12 月我在布拉格经济大学做的名为‘ Python 数据科学’讲座的笔记.欢迎通过 @RadimRehurek 进行提问和评论. 本次讲座的目的是展示一些关于机器学习的高级概念. ...

  7. mvc4 找到多个与名为“xx”的控制器匹配的类型

    asp.net mvc4 添加分区出现错误 找到多个与名为“home”的控制器匹配的类型 会出现如下错误”找到多个与名为“home”的控制器匹配的类型“ 在RouteConfig文件中添加命名空间可解 ...

  8. Storm元数据交互详解

    一.Nimbus Nimbus既需要在Zookeeper中创建元数据,也需要从Zookeeper中获取元数据. 如上图箭头1所示: 1.对于路径a,Nimbus只会创建路径,不会设置数据,数据是稍后由 ...

  9. 【Leetcode】179. Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  10. 【Leetcode】 328. Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...