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. maven课程 项目管理利器-maven 4-1 使用maven创建web项目 5星

    本节主要讲了使用maven创建web项目 主要分这三大类: 1 新建maven  web项目 2 后续处理普通java项目转web项目需要关注的点 3 maven特色转web需要关注的点 1 新建ma ...

  2. Hibernate课程 初探一对多映射3-2 单向多对一的配置

    1 多方实体类中加入,一方类和getset方法 //多方定义一个一方的引用 private Grade grade; public Grade getGrade() { return grade; } ...

  3. Dreams save us. Dreams lift us up and transform us into something better.

    Dreams save us. Dreams lift us up and transform us into something better.梦想能够拯救我们.梦想能够激励我们并让我们成为更好的人 ...

  4. dojo query 基本用法

    1. 常用的 dojo.query 用法 dojo.query("#header > h1") //ID 为 header 的元素的直接子节点中的 h3 元素   dojo. ...

  5. 初学:react-native 轮播图

    参考资料:http://reactscript.com/react-native-card-carousel-component/ import React, {Component} from 're ...

  6. 水晶报表分组,统计,求和,sum()函数使用

    --Sum()函数统计的是明细所有的和 Sum(字段名) --根据分组字段统计的和 Sum ({xh_Getdinggoudan;1.Djine} ,{xh_Getdinggoudan;1.Ddgda ...

  7. shell脚本学习(3)文件判断

    shell常用的文件判断运算符如下: -e 文件是否存在 -f  文件是否是普通文件(不是目录.设备文件.链接文件) -s  表示文件大小不为0 -d 表示文件是否是目录 -b 表示是块设备(光驱.软 ...

  8. 中兴ZXR10 6905核心交换机配置案例

    Connecting to 192.168.0.254:23...Connection established.To escape to local shell, press 'Ctrl+Alt+]' ...

  9. SqlServer-Cursor讲解一

    原创文章,转载必需注明出处:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/introduce-for-sqlserver-s-cu ...

  10. 22 Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...