题目链接

D. Bag of mice
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 exceed10 - 9.

Sample test(s)
input
1 3
output
0.500000000
input
5 5
output
0.658730159
Note

Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.

题意:

原来袋子里有w只白鼠和b只黑鼠
龙和王妃轮流从袋子里抓老鼠。谁先抓到白色老师谁就赢。
王妃每次抓一只老鼠,龙每次抓完一只老鼠之后会有一只老鼠跑出来。
每次抓老鼠和跑出来的老鼠都是随机的。
如果两个人都没有抓到白色老鼠则龙赢。王妃先抓。
问王妃赢的概率。

分析:

设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只白鼠

分析转载自: http://www.cnblogs.com/kuangbin/archive/2012/10/04/2711184.html

概率dp正推。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <algorithm>
#define LL __int64
const int maxn = 1e3 + ;
using namespace std;
double d[maxn][maxn]; int main()
{
int w, b, i, j;
while(~scanf("%d%d", &w, &b))
{
memset(d, , sizeof(d));
for(i = ; i <= w; i++) //一定要注意从1开始,不然初始化会出错,d[0][0]应该==0
d[i][] = 1.0;
for(i = ; i <= w; i++) //i和j也都要从1开始,不然因为下面第一个式子会重复计算
for(j = ; j <= b; j++)
{
d[i][j] += (double)i/(i+j);
if(j>=)
d[i][j] += (double)j/(i+j)*(double)(j-)/(i+j-)*(double)i/(i+j-)*d[i-][j-];
if(j>=)
d[i][j] += (double)j/(i+j)*(double)(j-)/(i+j-1.0)*(double)(j-)/(i+j-2.0)*d[i][j-];
}
printf("%.9lf\n", d[w][b]);
}
return ;
}

CF 148D D Bag of mice (概率dp)的更多相关文章

  1. Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题

    除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...

  2. CF 148D Bag of mice 概率dp 难度:0

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  3. codeforce 148D. Bag of mice[概率dp]

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  4. codeforces 148D Bag of mice(概率dp)

    题意:给你w个白色小鼠和b个黑色小鼠,把他们放到袋子里,princess先取,dragon后取,princess取的时候从剩下的当当中任意取一个,dragon取得时候也是从剩下的时候任取一个,但是取完 ...

  5. Bag of mice(概率DP)

    Bag of mice  CodeForces - 148D The dragon and the princess are arguing about what to do on the New Y ...

  6. Codeforces Round #105 (Div. 2) D. Bag of mice 概率dp

    题目链接: http://codeforces.com/problemset/problem/148/D D. Bag of mice time limit per test2 secondsmemo ...

  7. CF 148D D. Bag of mice (概率DP||数学期望)

    The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests ...

  8. Codeforces 148D Bag of mice 概率dp(水

    题目链接:http://codeforces.com/problemset/problem/148/D 题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢 ...

  9. 抓老鼠 codeForce 148D - Bag of mice 概率DP

    设dp[i][j]为有白老鼠i只,黑老鼠j只时轮到公主取时,公主赢的概率. 那么当i = 0 时,为0 当j = 0时,为1 公主可直接取出白老鼠一只赢的概率为i/(i+j) 公主取出了黑老鼠,龙必然 ...

随机推荐

  1. mongo 数据库提前关闭 避免读写任务没有结束,异步任务没有完成,同步指令提前关闭数据库:'MongoError: server instance pool was destroyed'

    mongo 数据库提前关闭 // mongodb - npm https://www.npmjs.com/package/mongodb const mongoCfg = { uri: 'mongod ...

  2. 【题解】 AT2134 Zigzag MST

    [题解]AT2134 Zigzag MST 一道MST好题 \(Anson\)有云: 要么是减少边的数量. 要么是改变连接边的方式. 那么如何减少边的数量呢?很简单,把所有不可能对答案产生贡献的边去掉 ...

  3. 基础学习笔记之opencv(6):实现将图片生成视频

    基础学习笔记之opencv(6):实现将图片生成视频 在做实验的过程中.难免会读视频中的图片用来处理,相反将处理好的图片又整理输出为一个视频文件也是非经常常使用的. 以下就来讲讲基于opencv的C+ ...

  4. java: new Date().getTime() 与 System.currentTimeMillis() 与 System.nanoTime()

    java使用new Date()和System.currentTimeMillis()获取当前时间戳   在开发过程中,通常很多人都习惯使用new Date()来获取当前时间,使用起来也比较方便,同时 ...

  5. POJ题目算法分类总结博客地址

    http://blog.csdn.net/sunbaigui/article/details/4421705 又从这个地址找了一些:http://blog.csdn.net/koudaidai/art ...

  6. JAVA-配置path环境

    配置path环境变量变的目的是什么? 控制台可以在任意的路径下都可以找到java的开发工具. 为了说明几个JDK环境变量的作用,这里先给出环境变量的定义 环境变量: 环境变量一般是指在操作系统中用来指 ...

  7. vs2015professional过期后登录微软账户仍然不能使用的解决方法

    今天安装了vs2015pro版,找到了一个可以正常使用的密钥 2015 pro(专业版)key:HMGNV-WCYXV-X7G9W-YCX63-B98R2 注意是专业版,非企业版 来源:https:/ ...

  8. tkinter之对话框

    对话框的一个例子: from tkinter.dialog import * from tkinter import * def investigation(): d=Dialog(None,titl ...

  9. linux C++ scandir 的使用

    () 头文件 #include <dirent.h> () 函数定义 int scandir(const char *dir,struct dirent **namelist,int (* ...

  10. HihoCoder1652 : 三角形面积和2([Offer收割]编程练习赛38)(几何)(不会几何,占位)

    描述 如下图所示,在X轴上方一共有N个三角形.这些三角形的底边与X轴重合,底边上两个顶点的坐标分别是(Li, 0)和(Ri, 0),底边的对顶点坐标是(Xi, Yi).其中Li ≤ Xi ≤ Ri 且 ...