Bag of mice(概率DP)
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 bblack 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.
Example
1 3
0.500000000
5 5
Output
0.658730159
题意: 公主和龙玩一个抓老鼠的游戏。袋子里,有两种老鼠,W只白老鼠,b只黑老鼠。一次抓出一只老鼠,公主先抓,龙后抓,龙抓出一只老鼠后,剩下的老鼠中会逃跑掉任意一只(跑掉的这只不算任何人抓的)。先抓到白老鼠的获胜(公主除抓到白老鼠获胜外,其余情况都算输),求公主获胜的概率。
题解:
思考: 对于 w 只白老鼠,b 只黑老鼠,公主要赢的情况
(一) 直接抓到一只白老鼠,概率为 p1 = w/(w+b)
(二) 抓到一只黑老鼠,但是龙也抓住一只黑老鼠,概率为
p2 = (1-p1)*(b-1)/(w+b-1) 然后跑掉一只老鼠,再分两种
跑掉一只白的 p3=w/(w+b-2) 变为 w-1 , b-2 的状态
跑掉一只黑的 p4=(b-2)/(w+b-2) 变为 w , b-3 的状态
dp[i][j] 代表 i 只白老鼠, j 只黑老鼠公主获胜的概率
dp[i][j]=p1 + p2*p3*dp[i-1][j-2] + p2*p3*dp[i][j-3];
#include <iostream>
#include <stdio.h>
using namespace std;
#define MAXN 1005
double dp[MAXN][MAXN]; void Init()
{
for (int i=;i<MAXN;i++)
{
for (int j=;j<MAXN;j++)
{
double p1=,p2=;
if (i>=)
p1 = (i*1.0)/(i+j); //公主赢
if (j>=)
p2 = (-p1)*(j-1.0)/(i+j-); //龙抓黑 double p3 = ,p4 = ;
if (i>=&&j>=) p3 = (i*1.0)/(i+j-);
if (j>=) p4 =(j-2.0)/(i+j-); dp[i][j]= p1;
if (j>=) dp[i][j]+=p2*p3*dp[i-][j-];
if (j>=) dp[i][j]+=p2*p4*dp[i][j-];
}
}
} int main()
{
Init();
int w,b;
scanf("%d%d",&w,&b);
printf("%.12lf\n",dp[w][b]);
return ;
}
Bag of mice(概率DP)的更多相关文章
- Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题
除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...
- 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 ...
- 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 ...
- 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 ...
- codeforces 148D Bag of mice(概率dp)
题意:给你w个白色小鼠和b个黑色小鼠,把他们放到袋子里,princess先取,dragon后取,princess取的时候从剩下的当当中任意取一个,dragon取得时候也是从剩下的时候任取一个,但是取完 ...
- Codeforces 148D Bag of mice 概率dp(水
题目链接:http://codeforces.com/problemset/problem/148/D 题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢 ...
- 抓老鼠 codeForce 148D - Bag of mice 概率DP
设dp[i][j]为有白老鼠i只,黑老鼠j只时轮到公主取时,公主赢的概率. 那么当i = 0 时,为0 当j = 0时,为1 公主可直接取出白老鼠一只赢的概率为i/(i+j) 公主取出了黑老鼠,龙必然 ...
- Codeforces Round #105 D. Bag of mice 概率dp
http://codeforces.com/contest/148/problem/D 题目意思是龙和公主轮流从袋子里抽老鼠.袋子里有白老师 W 仅仅.黑老师 D 仅仅.公主先抽,第一个抽出白老鼠的胜 ...
- codeforces105d Bag of mice ——概率DP
Link: http://codeforces.com/problemset/problem/148/D Refer to: http://www.cnblogs.com/kuangbin/archi ...
随机推荐
- linux 远程同步数据工具rsync (1)
rsync 远程同步数据工具,是linux下的数据备份工具rsync(remote sync 远程同步) 特点:在本地同步数据(类似于cp,但是有不同于远程scp),它会先判断已经存在的数据和远程数据 ...
- 批量删除Redis中的key
bin/redis-cli -h 192.168.46.151 -p 6379 keys "rulelist*" | xargs bin/redis-cli -h 192.168 ...
- CentOS 7.2安装lepus数据库监控系统
环境说明 系统版本 CentOS 7.2 x86_64 软件版本 lepus 3.7 Lepus是一套开源的数据库监控平台,目前已经支持MySQL.Oracle.SQLServer.MongoDB ...
- 【共享单车】—— React后台管理系统开发手记:城市管理和订单管理
前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...
- 移动web之响应式布局
1.响应式布局的概念 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念.简而言之.就是一个站点可以兼容多个终端--而不是为每一个终端做一个特定的版本号. 这个概念是为解决移动互 ...
- Sublime Text 3 文档
中文版:http://feliving.github.io/Sublime-Text-3-Documentation/ 英文版:http://www.sublimetext.com/docs/3/
- hibernate学习系列-----(5)hibernate基本查询下篇:hibernate聚合函数、分组查询及命名查询
在上一篇中,大致学习了hibernate的基本查询:HQL基本查询,今天,继续昨天的步伐,继续学习hibernate的基本查询..... 1.hql聚合函数,先大致列一下hql的聚合函数有哪些吧: 在 ...
- 倍福TwinCAT(贝福Beckhoff)基础教程2.2 TwinCAT常见类型使用和转换_函数块
右击POUs,添加一个FB功能块,相比于FUN,FB功能块有INPUT,OUTPUT,还有VAR,即FB可以有多个输出,但是整个FB没有返回值 实现相同的功能,FB要比FUN难看的多,FB要声明实 ...
- CCF计算机职业资格认证 2015年3月第2题 数字排序 解法和思路
问题描写叙述 给定n个整数,请统计出每一个整数出现的次数.按出现次数从多到少的顺序输出. 输入格式 输入的第一行包括一个整数n,表示给定数字的个数. 第二行包括n个整数.相邻的整数之间用一个空格分隔, ...
- PHP中session详解
SESSION 的数据保存在哪里呢? 当然是在服务器端,但不是保存在内存中,而是保存在文件或数据库中. 默认情况下,PHP.ini 中设置的 SESSION 保存方式是 files(session ...