Bag of mice

The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance.

They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning?

If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one.

Input

The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000).

Output

Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9.

Examples

Input
1 3
Output
0.500000000
Input
5 5
Output
0.658730159

题意:一对情侣玩抓老鼠游戏,老鼠有黑白两色,女生为先手,先抓到白老鼠胜。 特别的,若两人均未抓到则算男生赢,且两人抓完后会跑走一只老鼠,还会随机放走一只老鼠,问女生赢的概率是多少。

解析:

设dp[i][j]表示现在轮到女生抓时有i只白鼠,j只黑鼠,女赢的概率
明显 dp[0][j]=0,0<=j<=b;因为没有白色老鼠了
dp[i][0]=1,1<=i<=w;因为都是白色老鼠,抓一次肯定赢了。
dp[i][j]可以转化成下列四种状态:
1、女生抓到一只白鼠,则女赢了,概率为i/(i+j);
2、女生抓到一只黑鼠,男生抓到一只白色,则女输了,概率为j/(i+j)*i/(i+j-1).
3、女生抓到一只黑鼠,男生抓到一只黑鼠,跑出来一只黑鼠,则转移到dp[i][j-3]。
概率为j/(i+j)*(j-1)/(i+j-1)*(j-2)/(i+j-2);
4、女生抓到一只黑鼠,男生抓到一只黑鼠,跑出来一只白鼠,则转移到dp[i-1][j-2].
概率为j/(i+j)*(j-1)/(i+j-1)*i/(i+j-2); 当然后面两种情况要保证合法,即第三种情况要至少3只黑鼠,第四种情况要至少2只白鼠
*/
代码:
 #include"bits/stdc++.h"

 #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, n) for(int i=0;i<n;i++)
using namespace std;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const ll INF = 0x3fffffffffffffff; db dp[][];
int t,n,m;
int main()
{
ci(n),ci(m);
for(int i=0;i<=m;i++) dp[][i]=;
for(int i=;i<=n;i++) dp[i][]=;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++)
{
dp[i][j]+=(db)i/(i+j);
if(j>=) dp[i][j]+=(db)j/(i+j)*(db)(j-)/(i+j-)*(db)i/(i+j-)*dp[i-][j-];
if(j>=) dp[i][j]+=(db)j/(i+j)*(db)(j-)/(i+j-)*(db)(j-)/(i+j-)*dp[i][j-];
}
}
printf("%.9f\n",dp[n][m]);
return ;
}

 

Codeforces146D 概率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. MySQL(三) 完整性约束

    一.介绍 约束条件与数据类型的宽度意义,都是可选参数. 作用:用于保证数据的完整性和一致性. 主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN ...

  2. php socket简单使用

    php的socket编程算是比较难以理解的东西吧,不过,我们只要理解socket几个函数之间的关系,以及它们所扮演的角色,那么理解起来应该不是很难了,在笔者看来,socket编程,其实就是建立一个网络 ...

  3. VueJs组件prop验证简单理解

    今天看了vuejs的组件,看到了prop组件,主要作用是在传入数据的时候对传入的值做判断,写了个小例子. <div id="app"> <my-child :nu ...

  4. mac笔记本上的工具

    svn可是换工具:cornerstone host修改工具:switchHosts!

  5. (13)JavaScript之[HTML DOM元素][JS对象]

    元素 /** * HTML DOM 元素(节点)*/ //创建新的HTML元素 var para = document.createElement('p'); var node = document. ...

  6. SQL Server 2008数据备份与还原的原理是什么?

    为什么SqlServer有完整备份.差异备份和事务日志备份三种备份方式,以及为什么数据库又有简单模式.完整模式和大容量日志模式这三种恢复模式.本文内容适用于2005以上所有版本的SqlServer数据 ...

  7. vim复制粘贴到系统剪贴板

    一般来讲,如果你没有在.vimrc中配置过相关的信息的话,可以考虑下面的方法.系统环境 Ubuntu 14.04 LTS. 安装与使用 首先需要安装一个vim-gtk 命令$sudo apt-get ...

  8. Html + JS : 点击对应的按钮,进行选择是隐藏还是显示(用户回复功能)

    例如: 当我点击按钮1时,点击第一下进行显示This is comment 01,点击第二下隐藏This is comment 01 当我点击按钮2时,点击第一下进行显示This is comment ...

  9. IOS 数据加密方式(加盐,MD5加密,)

    加密方式封装 @interface NSString (Hash) @property (readonly) NSString *md5String; @property (readonly) NSS ...

  10. 如何实现SQL Server临时表的创建?

    以下的文章主要是对SQL Server临时表的创建的实际操作步骤,以及在实际操作中我们要用到的实际应用代码的介绍,我在一个信誉度很好的网站找到一个关于其相关内容今天拿出来供大家分享. 创建临时表 方法 ...