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. OpenLDAP配置TLS加密传输

    原文发表于cu:2016-07-04 参考文档: 基于OpenSSL自建CA与颁发SSL证书:http://seanlook.com/2015/01/18/openssl-self-sign-ca/ ...

  2. 常用DOS指令备忘

    1.删除整个目录,包括空目录 rd D:\管理\2012新同学练习\.svn /s/q /s 删除当前目录及子目录 /q 不询问直接删除 2.拷贝目录树 xcopy D:\管理\2012新同学练习 E ...

  3. [转]如何设计自适应屏幕大小的网页 Responsive Web Design

    随着3G的普及,越来越多的人使用手机上网. 移动设备正超过桌面设备,成为访问互联网的最常见终端.于是,网页设计师不得不面对一个难题:如何才能在不同大小的设备上呈现同样的网页? 手机的屏幕比较小,宽度通 ...

  4. Improving the Safety, Scalability, and Efficiency of Network Function State Transfers

    Improving the Safety, Scalability, and Efficiency of Network Function State Transfers 来源:ACM SIGCOMM ...

  5. HDU 5666 Segment 数论+大数

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5666 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  6. JDK源码分析 – HashMap

    HashMap类的申明 HashMap的定义如下: public class HashMap<K,V> extends AbstractMap<K,V> implements ...

  7. 词法分析用c++实现的

    #include<stdio.h>#include<string.h>int i,j,k,sign,flag,number,run;char ch;char word[10]; ...

  8. 软工网络15团队作业4-DAY3

    昨天的工作. 张陈东芳:数据库连接的检查 吴敏烽:商品实体类的检查 周汉麟:继续研究获取商品信息方法的方法和调试 林振斌:继续研究获取商品信息方法的方法和调试 李智:Cookies的检查 全体人员:优 ...

  9. QtCharts模块在QtWideget中图表绘制(非QML)

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QtCharts模块在QtWideget中图表绘制(非QML)     本文地址:http:/ ...

  10. An In-Depth Look at the HBase Architecture--转载

    原文地址:https://www.mapr.com/blog/in-depth-look-hbase-architecture In this blog post, I’ll give you an ...