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: NMP. 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)的更多相关文章

  1. Codeforces 28C [概率DP]

    /* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...

  2. HDU 4405 Aeroplane chess (概率DP)

    题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i  这个位置到达 n ...

  3. POJ 2096 Collecting Bugs (概率DP)

    题意:给定 n 类bug,和 s 个子系统,每天可以找出一个bug,求找出 n 类型的bug,并且 s 个都至少有一个的期望是多少. 析:应该是一个很简单的概率DP,dp[i][j] 表示已经从 j ...

  4. POJ 2151 Check the difficulty of problems (概率DP)

    题意:ACM比赛中,共M道题,T个队,pij表示第i队解出第j题的概率 ,求每队至少解出一题且冠军队至少解出N道题的概率. 析:概率DP,dp[i][j][k] 表示第 i 个队伍,前 j 个题,解出 ...

  5. 概率DP light oj 1030

    t组数据 n块黄金 到这里就捡起来 出发点1 到n结束  点+位置>n 重掷一次 dp[i] 代表到这里的概率 dp[i]=(dp[i-1]+dp[i-2]... )/6  如果满6个的话 否则 ...

  6. hdu 4050 2011北京赛区网络赛K 概率dp ***

    题目:给出1-n连续的方格,从0开始,每一个格子有4个状态,左右脚交替,向右跳,而且每一步的步长必须在给定的区间之内.当跳出n个格子或者没有格子可以跳的时候就结束了,求出游戏的期望步数 0:表示不能到 ...

  7. [转]概率DP总结 by kuangbin

    概率类题目一直比较弱,准备把kuangbin大师傅总结的这篇题刷一下! 我把下面的代码换成了自己的代码! 原文地址:http://www.cnblogs.com/kuangbin/archive/20 ...

  8. SGU 422 Fast Typing(概率DP)

    题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...

  9. HDU 4050 wolf5x(动态规划-概率DP)

    wolf5x Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

随机推荐

  1. 20151212jquery学习笔记--工具函数

    工具函数是指直接依附于 jQuery 对象,针对 jQuery 对象本身定义的方法,即全局性 的函数.它的作用主要是提供比如字符串.数组.对象等操作方面的遍历. 一.字符串操作 在 jQuery 中, ...

  2. C# 类是怎么执行的?

    C# 类是怎么执行的? public class Person{ static person(){} //不写,默认也有个空的 public person(){}//不写,默认也有个空的 public ...

  3. JAVA长连接demo

    http://blog.csdn.net/caomiao2006/article/details/38830475 JAVA长连接demo 2014-08-25 23:20 4767人阅读 评论(2) ...

  4. ios隐藏导航栏底线条和导航、状态栏浙变色

    方法一遍历法: 在你需要隐藏的地方调用如下代码 [self findlineviw:self.navigationBar].hidden = YES; -(UIImageView*)findlinev ...

  5. java线程中生产者与消费者的问题

    一.概念 生产者与消费者问题是一个金典的多线程协作的问题.生产者负责生产产品,并将产品存放到仓库:消费者从仓库中获取产品并消费.当仓库满时,生产者必须停止生产,直到仓库有位置存放产品:当仓库空时,消费 ...

  6. scrapy, 自带命令行调用工具.

    #-*- coding:utf-8 -*- from scrapy import cmdline cmdline.execute("scrapy crawl dmoz".split ...

  7. 04_XML_02_XML语法

    [XML组成] 一个XMl文件分为以下几部分组成 * 文档说明 * 元素 * 属性 * CDATA区.特殊字符 * 处理指令(processing Instruction) [1.文档说明] * 最简 ...

  8. 在阿里云服务器ubuntu14.04运行netcore

    从netcore1.0正式发布就很激动,想要赶紧学习. 最近博客园的一篇文章给了完整的指导非常感谢,但是在实际实现到发布到阿里云服务器遇到一些问题,记录下来. 首先上基础文章http://www.cn ...

  9. shell sort

    sort is awesome! 22 hpp_national:talk:recentAtUsers-64490 23 hpp_national:talk:site_online_users:178 ...

  10. 两个for循环例子

    var i,j; var a=0; // for(i=0,j=0;i<5,j<7;i++,j++){ // a=i+j; // } // alert(a) //12 for(i=0,j=0 ...