ZOJ3582:Back to the Past(概率DP)
Recently poet Mr. po encountered a serious problem, rumor said some of his early poems are written by others. This brought a lot of trouble to Mr. po, so one day he went to his best friend MasterO for help. MasterO handed over a small wooden box with a silent smile. Mr. po checked the box, a word "YGBH" carved on the side. "The box can take you back to the past," MasterO said, "so you can get any evidence you need. But, before that, you need some patience."
There are N tiny dark holes on both sides of the box (2N holes in total). Every day, for each hole, there is a possibility P to successfully absorb the power of moon and then magically sparkle. The possibilities among holes are independent in each day. Once a hole sparkles, it will never turn dark again. The box only works when there are no less than M sparkling holes on each side of the box. The question is that what is the expected number of days before the box is available.
Input
The input consists of several test cases. For each case there are 3 numbers in a line: N, M, P. 1 ≤ N ≤ 50, 1 ≤ M ≤ N, 0.01 ≤ P ≤ 1. A case with three zeros indicates the end of the input.
Output
For each test case, output the expected number of days, accurate up to six decimal places.
Sample Input
2 1 1
1 1 0.5
0 0 0
Sample Output
1.000000
2.666667
题意:给出n,m,p,盒子两边各有n盏灯,要求两边都至少亮m盏灯的期望,每个灯的亮暗都是独立的,亮的概率为p,亮了的灯不会再熄灭
思路:又是一道概率DP求期望的问题,设dp[i][j]表示左边i个亮,右边j个亮到达目标的期望
则dp[i][j]=1+sigma{C(n-i,x)*p^x*(1-p)^(n-i-x) * C(n-j,y)*p^y*(1-p)^(n-j-y)*dp[i+x][j+y]};
注意这里的C(n-i,x)是一个组合数,这些组合数可以用杨辉三角的性质对它们进行打表处理
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std; double dp[55][55],c[55][55];
double yes[55],no[55];
//dp[i][j]表示一边亮i个洞,一边亮j个洞到达成目标还需的天数
void set()//杨辉三角打表,也就是二项式各项系数
{
int i,j;
for(i = 0; i<=55; i++)
{
c[i][0] = c[i][i] = 1;
for(j = 1; j<i; j++)
c[i][j] = c[i-1][j-1]+c[i-1][j];
}
} int main()
{
int n,m,i,j,x,y;
double p,sum;
set();
while(~scanf("%d%d%lf",&n,&m,&p))
{
if(n==0 && m==0)
break;
yes[0] = no[0] = 1;
for(i = 1; i<=n; i++)
{
yes[i] = yes[i-1]*p;
no[i] = no[i-1]*(1.0-p);
}
memset(dp,0,sizeof(dp));
for(i = n; i>=0; i--)
{
for(j = n; j>=0; j--)
{
//i,j,代表左右已亮的个数
if(i>=m && j>=m)
continue;
sum = 0;
for(x = 0; x+i<=n; x++)
{
for(y = 0; y+j<=n; y++)
{
if(x==0 && y==0)
continue;
if(n-i>=x && n-j>=y)
sum+=dp[x+i][y+j]*c[n-i][x]*yes[x]*no[n-i-x]*c[n-j][y]*yes[y]*no[n-j-y];
//x,y是左右新增的亮起来的个数
//c[n-i][x],就是在剩下的n-i个中选x个作为新亮起来的灯,也就是一个组合数
}
}
dp[i][j] = (sum+1.0)/(1.0-no[n-i]*no[n-j]);//求期望,+1是一天过了,除以(1.0-no[n-i]*no[n-j])是至少亮了一盏
}
}
printf("%.6f\n",dp[0][0]);
} return 0;
}
ZOJ3582:Back to the Past(概率DP)的更多相关文章
- Codeforces 28C [概率DP]
/* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...
- HDU 4405 Aeroplane chess (概率DP)
题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i 这个位置到达 n ...
- POJ 2096 Collecting Bugs (概率DP)
题意:给定 n 类bug,和 s 个子系统,每天可以找出一个bug,求找出 n 类型的bug,并且 s 个都至少有一个的期望是多少. 析:应该是一个很简单的概率DP,dp[i][j] 表示已经从 j ...
- POJ 2151 Check the difficulty of problems (概率DP)
题意:ACM比赛中,共M道题,T个队,pij表示第i队解出第j题的概率 ,求每队至少解出一题且冠军队至少解出N道题的概率. 析:概率DP,dp[i][j][k] 表示第 i 个队伍,前 j 个题,解出 ...
- 概率DP light oj 1030
t组数据 n块黄金 到这里就捡起来 出发点1 到n结束 点+位置>n 重掷一次 dp[i] 代表到这里的概率 dp[i]=(dp[i-1]+dp[i-2]... )/6 如果满6个的话 否则 ...
- hdu 4050 2011北京赛区网络赛K 概率dp ***
题目:给出1-n连续的方格,从0开始,每一个格子有4个状态,左右脚交替,向右跳,而且每一步的步长必须在给定的区间之内.当跳出n个格子或者没有格子可以跳的时候就结束了,求出游戏的期望步数 0:表示不能到 ...
- [转]概率DP总结 by kuangbin
概率类题目一直比较弱,准备把kuangbin大师傅总结的这篇题刷一下! 我把下面的代码换成了自己的代码! 原文地址:http://www.cnblogs.com/kuangbin/archive/20 ...
- SGU 422 Fast Typing(概率DP)
题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...
- HDU 4050 wolf5x(动态规划-概率DP)
wolf5x Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
随机推荐
- 将Excel,ppt和word转化为html
有些时候可能需要将Excel,ppt和word转化为html在页面上显示.我从网上查到一些代码,记录在这里以供需要的朋友参考 1.将word转化为html显示 //================== ...
- ios-异步消息同步问题-典型使用场景: 微信私信界面
前言 在ios开发中常常会有聊天功能,一般简单聊天功能只传输文字,但是稍微复杂点儿会有图片发送功能了.最全而且可支持扩展的例如微信,qq 聊天功能了.传输方式各有千秋,如get,post,websoc ...
- 周末充电之WPF(二 ) .窗口的布局
登录窗口布局:[ Grid 布局 -Grid.RowDefinitions / Grid.ColumnDefinitions] 代码如下: <Window x:Class="login ...
- ios 64位下编译webrtc的libvpx库出现错误Bad cputype for object file.Currently only tested for CPU_TYPE_x86_64
diff --git a/libvpx.gyp b/libvpx.gypindex 4f8cb2b..4eb6866 100644--- a/libvpx.gyp+++ b/libvpx.gyp@@ ...
- 【CF493E】【数学】Vasya and Polynomial
Vasya is studying in the last class of school and soon he will take exams. He decided to study polyn ...
- 【转】JSON简介以及用法代码汇总
什么是JSON? JavaScript 对象表示法(JavaScript Object Notation). JSON是一种轻量级的数据交换格式,某个JSON格式的文件内部譬如可以长成这样: { &q ...
- javaScript中with的用法
1 JavaScript中的with语句的作用是为逐级的对象访问提供命名空间式的速写方式, 也就是在指定的代码区域, 直接通过节点名称调用对象 初次接触到with用法,是这样一段代码: 1 2 3 4 ...
- 在Yii2.0中实现计划任务(cron)
以下由我们在信易网络公司开发项目的时候终结出的一些经验 Create console application 创建命令行应用 In advance template there is already ...
- 服务器慢 mysql-bin.000001文件占满磁盘的原因与解决
发现 VPS 服务器上的网站反应超级慢,简单的重启.重启各主要服务,发现mysql 的反应极其不正常. 一方面是问题,这与站点访问量有关.开始时从mysql 的配置文件 my.cnf 考虑,但志文工作 ...
- 【转】成为Java顶尖程序员 ,看这11本书就够了
成为Java顶尖程序员 ,看这11本书就够了 转自:http://developer.51cto.com/art/201512/503095.htm 以下是我推荐给Java开发者们的一些值得一看的好书 ...