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 ...
随机推荐
- Container.ItemIndex 获取到行的序号
如果在ASP.NET中应用了Repeater.Gridview,想获取到行的序号,很简单,使用Container.ItemIndex即可.在Gridview中使用<%# Container.Da ...
- store procedure 翻页
store procedure 翻页例子 .turn page CREATE PROCEDURE pageTest --用于翻页的测试 --需要把排序字段放在第一列 ( )=null, --当前页面里 ...
- (转)Cookies使用
实际上,在web开发中,cookie仅仅是一个文本文件,当用户访问站点时,它就被存储在用户使用的计算机上,其中,保存了一些信息,当用户日后再次访问这个站点时,web可以将这些信息提取出来. 尽管现在听 ...
- java- 枚举的常见用法
用法一:常量 public enum MyColor{Red,Black,Blue} public enum Color { RED, GREEN, BLANK, YELLOW } enum为枚举类的 ...
- iOS 开发中的单例
在iOS开发中经常会用到单例,比如每个iOS程序本身就是一个单例,在比如进行个人偏好设置存储的时候用的也是一个单例.那我们如何自己来写一个单例类呢,用自己的单例对象呢?下面是我写的一个单例的头文件里的 ...
- 淘宝可以传照片搜索商品,verygood.雅客VC多味水果糖
奶奶喜欢吃点硬糖.在当地买了些说是不好.到是一个亲戚买的一种糖比较满意(好久了都快融化了). 但是我只有照片,能知道品牌,在jd没这样一样的商品了. 还好借助淘宝的传照片功能,找到了.
- HDU 3681 Prison Break(状态压缩dp + BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...
- Top命令查看内存
c 切换显示命令名称和完整命令行. M 根据驻留内存大小进行排序 第四行:内存状态 8306544k total — 物理内存总量(8GB) 7775876k used — 使用中的内存总量(7.7G ...
- AJAX 一些常用方法
abort() 停止当前请求getAllResponseHeaders() 返回包含HTTP请求的所有响应头信息,其中响应头包括Content-Length,Date,URI等内容.getRespon ...
- 安装grid之前检查配置 ,报错如下
centos 5 _x86_64 oracle 11.2 安装grid之前检查配置 ,报错如下 : ./runcluvfy.sh stage -pre crsinst -n rac1,rac2 -fi ...