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. 20160417javaweb之servlet监听器

    监听器:监听器就是一个java程序,功能是监听另一个java对象变化(方法调用.属性变更) 8个监听器,分为了3种 写一个类实现响应的接口 注册监听器 -- 在web.xml中注册监听器 1.用来监听 ...

  2. .net的 async 和 await

    async 和 await 出现在C# 5.0之后,关系是两兄弟,Task是父辈,Thread是爷爷辈,这就是.net 多线程处理的东西,具体包括 创建线程,线程结果返回,线程中止,线程中的异常处理 ...

  3. 学习笔记_JDBC_1_Demo1_连接数据库的基本操作和步骤

    常见错误:连数据库时,这句话String url = "jdbc:mysql://localhost/数据库名";数据库名可能和你建的数据库名不一样 1.安装Mysql,记住此时你 ...

  4. 263. Ugly Number(C++)

    263. Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are ...

  5. SETLOCAL

    Quote from: http://ss64.com/nt/setlocal.html SETLOCAL Set options to control the visibility of envir ...

  6. SQL的经典操作——批量复制同行的其它列数据到其它列数据

    看图说话比较直观: 对比复制前后的数据表: 使用SQL语句:UPDATE OR ROLLBACK Content SET YINBIAO = YINBIAO2, GESHU = GESHU2 WHER ...

  7. Java面向对象程序设计--与C++对比说明:系列3(Java 继承机制)

    继承(inheritance)背后的核心思想是:       bonus = b;    }      } Java没有像C++那样提供多继承机制,但提供了接口机制,在后面我们将详细探究接口机制的实现 ...

  8. 专题二、ArrayList序列化技术细节详解

    一.绪论 所谓的JAVA序列化与反序列化,序列化就是将JAVA 对象以一种的形式保持,比如存放到硬盘,或是用于传输.反序列化是序列化的一个逆过程. JAVA规定被序列化的对象必须实现java.io.S ...

  9. std::string stringf(const char* format, ...)

    std::string stringf(const char* format, ...){ va_list arg_list; va_start(arg_list, format); // SUSv2 ...

  10. 快速设置IP的脚本

    @echo off cls ::set NAME="本地连接" set NAME="无线网络连接" set IP=192.168.1.55 set MASK=2 ...