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 secondsmemory limit per test256 megabytes
#### 问题描述
> 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.
输入
The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000).
输出
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.
样例输入
1 3
样例输出
0.500000000
题意
公主和龙博弈,一个箱子里面有w只白色的老鼠,b只黑色的老鼠,每一轮可以向箱子里面随机抓一只老鼠,如果抓出来的是白鼠,则直接胜出,否则换人,龙抓的时候会同时下跑一只箱子里面的老鼠,这只老鼠也是随机的一只,公主比较温柔,不会下走额外的老鼠。公主先开始抓,问公主胜出的概率。
题解
dp[i][j][0]:表示现在是公主抓,并且箱子里面还剩i只白鼠、j只黑鼠的概率。
dp[i][j][1]:表示现在是龙抓,并且箱子里面还剩i只白鼠、j只黑鼠的概率。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-9;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1111;
int n;
int w,b;
double dp[maxn][maxn][2];
void init(){
clr(dp,0);
}
int main() {
init();
scf("%d%d",&w,&b);
dp[w][b][1]=0;
dp[w][b][0]=1;
double ans=0.0;
for(int i=w;i>=0;i--){
for(int j=b;j>=0;j--){
///必输
if(i==0){ continue; }
///这局分不出胜负
if(j-1>=0) dp[i][j-1][1]+=j*1.0/(i+j)*dp[i][j][0];
///这局就赢的概率
ans+=i*1.0/(i+j)*dp[i][j][0];
///抓到一只黑的,同时跑了一只黑的
if(j-2>=0) dp[i][j-2][0]+=1.0*j*(j-1)/((i+j)*(i+j-1))*dp[i][j][1];
///抓到一只黑的,同时跑了一只白的
if(j-1>=0) dp[i-1][j-1][0]+=1.0*i*j/((i+j)*(i+j-1))*dp[i][j][1];
}
}
prf("%.9lf\n",ans);
return 0;
}
//end-----------------------------------------------------------------------
记忆化搜索:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef int LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=10000000000000000LL;
const double eps=1e-9;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1111;
int w,b;
double dp[maxn][maxn][2];
double dfs(int x,int y,int z){
if(x<=0||y<0) return 0;
if(dp[x][y][z]>=eps) return dp[x][y][z];
double& res=dp[x][y][z]=0;
if(z==0){
res+=1.0*x/(x+y);
if(y>0) res+=1.0*y/(x+y)*dfs(x,y-1,z^1);
}else{
if(y>1) res+=1.0*y/(x+y)*(y-1)/(x+y-1)*dfs(x,y-2,z^1);
if(y>0) res+=1.0*y/(x+y)*x/(x+y-1)*dfs(x-1,y-1,z^1);
}
return res;
}
int main() {
clr(dp,-1);
scf("%d%d",&w,&b);
prf("%.9lf\n",dfs(w,b,0));
return 0;
}
//end-----------------------------------------------------------------------
Codeforces Round #105 (Div. 2) D. Bag of mice 概率dp的更多相关文章
- Codeforces Round #301 (Div. 2) D. Bad Luck Island 概率DP
D. Bad Luck Island Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/pr ...
- Codeforces Round #293 (Div. 2) D. Ilya and Escalator 概率DP
D. Ilya and Escalator time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #284 (Div. 2) D. Name That Tune [概率dp]
D. Name That Tune time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #233 (Div. 2)D. Painting The Wall 概率DP
D. Painting The Wall ...
- Codeforces Round #597 (Div. 2) E. Hyakugoku and Ladders 概率dp
E. Hyakugoku and Ladders Hyakugoku has just retired from being the resident deity of the South Black ...
- 水题 Codeforces Round #105 (Div. 2) B. Escape
题目传送门 /* 水题:这题唯一要注意的是要用double,princess可能在一个小时之内被dragon赶上 */ #include <cstdio> #include <alg ...
- Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题
除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- Codeforces Round #105 (Div. 2) ABCDE
A. Insomnia cure 哎 只能说英语太差,一眼题我看了三分钟. 题意:给5个数k, l, m, n 和 d,求1~d中能被k, l, m, n 至少一个整除的数的个数. 题解:…… 代码: ...
随机推荐
- MongoDB Python官方驱动 PyMongo 的简单封装
最近,需要使用 Python 对 MongodB 做一些简单的操作,不想使用各种繁重的框架.出于可重用性的考虑,想对 MongoDB Python 官方驱动 PyMongo 做下简单封装,百度一如既往 ...
- day 85 Vue学习之vue-cli脚手架下载安装及配置
1. 先下载node.js,下载地址:https://nodejs.org/en/download/ 找个目录保存,解压下载的文件,然后配置环境变量,将下面的路径配置到环境变量中. 由于 Node ...
- Python的scrapy之爬取6毛小说网的圣墟
闲来无事想看个小说,打算下载到电脑上看,找了半天,没找到可以下载的网站,于是就想自己爬取一下小说内容并保存到本地 圣墟 第一章 沙漠中的彼岸花 - 辰东 - 6毛小说网 http://www.6ma ...
- 20145234黄斐《Java程序设计》实验五—网络安全与编程
1: 两人一组结对编程: 0. 参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA 1. 结对实现中缀表达式转后缀表达式的功能 MyBC.jav ...
- WPF 学习笔记-设置属性使窗口不可改变大小
原文:WPF 学习笔记-设置属性使窗口不可改变大小 调整Windows下的ResizeMode属性: ResizeMode = NoResize Resize属性是控制Windows是否可以改变大小, ...
- sqlserver 抓取所有执行语句 SQL语句分析 死锁 抓取
原文:sqlserver 抓取所有执行语句 SQL语句分析 死锁 抓取 在多人开发中最头疼的是人少事多没有时间进行codereview,本来功能都没时间写,哪有时间来开会细细来分析代码.软件能跑就行, ...
- PostreSQL崩溃试验全记录
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL基础知识与基本操作索引页 回到顶级页面:PostgreSQL索引页 [作者 高健@博客园 luckyjackg ...
- PyQt5 笔记(02):嵌套布局
如前一篇笔记,我们还是只讨论两层嵌套布局的情况. 前面的布局有一个缺点:有三个内层布局,则需要三个空部件.那若有十个内层布局呢?显然会让人不舒服. 刚才在玩 Qt Designer 时,发现了一个更好 ...
- easybcd删除win10启动项如何恢复?
制作windows启动盘: u盘启动 开始安装,选择左下角的"修复计算机": 运行命令 bcdboot c:\windows /l zh-cn 从系统盘C:\Windows目录中复 ...
- 一维码UPC E简介及其解码实现(zxing-cpp)
UPC(Universal Product Code)码是最早大规模应用的条码,其特性是一种长度固定.连续性的条 码,目前主要在美国和加拿大使用,由于其应用范围广泛,故又被称万用条码. UPC码仅可 ...