CF 148D. Bag of mice (可能性DP)
2 seconds
256 megabytes
standard input
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.
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 exceed10 - 9.
1 3
0.500000000
5 5
0.658730159
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.
採用了搜索记忆化的思想,每次的问题向下归结为较小的子问题求解。
注意一下精度问题,假设精度已经达到要求,就不用再向下进行求解了。
dp[w][b]=p1+p2*tmp;
p1=w/(w+b) //公主直接赢的概率
p2=b/(w+b), b--; p*=(b/(w+b)); b--; 经过一轮,两个选手均未摸到白老鼠
tmp=dfs(w-1,b)*(w/(w+b))+dfs(w,b-1)*(b/(w+b)); //向下深搜乘以对应的概率,某种颜色老鼠吓跑的概率。。
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 1005
#define LL __int64
const int inf=0x1f1f1f1f;
const double eps=1e-10;
double f[N][N];
double dfs(int w,int b) //Princess赢的概率
{
if(w<=0)
return 0;
if(b<=0)
return 1;
if(fabs(f[w][b]+1.0)>eps)
return f[w][b];
double p1,p2,tmp=1;
p1=w*1.0/(w+b); //直接赢
p2=b*1.0/(w+b); //还没输
b--;
p2=p2*(b*1.0/(w+b)); //dragon 也没赢
b--;
if(p2>eps) //精度不够则继续向下深搜!!
tmp=(w*1.0/(w+b))*dfs(w-1,b)+(b*1.0/(w+b))*dfs(w,b-1);
//printf("%.9f %.9f %.9f\n",p1,p2,tmp);
return f[w][b+2]=p1+p2*tmp;
}
int main()
{
int i,w,b;
memset(f,-1,sizeof(f));
while(scanf("%d%d",&w,&b)!=-1)
{
printf("%.9f\n",dfs(w,b));
}
return 0;
}
非递归形式的:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 1005
#define LL __int64
const int inf=0x1f1f1f1f;
const double eps=1e-10;
double dp[N][N]; //dp[w][b] 代表当前情况下Princess赢的概率
void inti()
{
memset(dp,-1,sizeof(dp));
dp[0][0]=0;
for(int i=1;i<N;i++)
{
dp[i][0]=1;
dp[0][i]=0;
}
}
int main()
{
int i,j,w,b;
double tmp;
inti();
while(scanf("%d%d",&w,&b)!=-1)
{
if(fabs(dp[w][b]+1.0)>eps)
{
printf("%.9f\n",dp[w][b]);
continue;
}
for(i=1;i<=w;i++)
{
for(j=1;j<=b;j++)
{
dp[i][j]=i*1.0/(i+j);
tmp=j*1.0/(i+j)*(j-1)/(i+j-1);
if(j>=2&&i+j>3)
dp[i][j]+=tmp*dp[i-1][j-2]*i/(i+j-2);
if(j>=3&&i+j>3)
dp[i][j]+=tmp*dp[i][j-3]*(j-2)/(i+j-2);
}
}
printf("%.9f\n",dp[w][b]);
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
CF 148D. Bag of mice (可能性DP)的更多相关文章
- 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 ...
- CF 148D Bag of mice【概率DP】
D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes Promblem descriptio ...
- 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) 公主取出了黑老鼠,龙必然 ...
- CF 148D Bag of mice 题解
题面 这是我做的第一道概率DP题: 做完后发现没有后效性的DP是真的水: 在这里说主要是再捋顺一下思路: 设f[i][j]表示有i只白鼠,j只黑鼠是获胜的概率: 显然:f[i][0]=1; 然后分四种 ...
- Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题
除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...
- Bag of mice(概率DP)
Bag of mice CodeForces - 148D The dragon and the princess are arguing about what to do on the New Y ...
随机推荐
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- ASA基本配置
拓扑如下: ASA5520# show running-config : Saved:ASA Version 8.0(2) !hostname ASA5520enable password 2KFQn ...
- python实现人人网用户数据爬取及简单分析
这是之前做的一个小项目.这几天刚好整理了一些相关资料,顺便就在这里做一个梳理啦~ 简单来说这个项目实现了,登录人人网并爬取用户数据.并对用户数据进行分析挖掘,终于效果例如以下:1.存储人人网用户数据( ...
- ORACLE中%TYPE和%ROWTYPE的使用
1 %TYPE说明 为了使一个变量的数据类型与还有一个已经定义了的变量(尤其是表的某一列)的数据类型相一致,Oracle提供了%TYPE定义方式.当被參照的那个变量的数据类型改变了之后,这个新定 ...
- Android中G-Sensor相关流程
1.使G-sensor正常工作需要做的事: G-sensor driver文件包括: driver/i2c/chips/lis331dl.c driver/i2c/chips/sensorioctl. ...
- C#的百度地图开发(二)转换JSON数据为相应的类
原文:C#的百度地图开发(二)转换JSON数据为相应的类 在<C#的百度地图开发(一)发起HTTP请求>一文中我们向百度提供的API的URL发起请求,并得到了返回的结果,结果是一串JSON ...
- dell N5010
Inspiron N5010Microsoft Windows 10 企业版 (64位) (英特尔)Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz(24 ...
- hdoj 1395 2^x mod n = 1 【暴力】
策略 : 观察可知,1 或者是能被2整除的数都不会求余等于1, 仅仅须要推断一下是不是除1之外的奇数,在依次查找2^x(mod(n)) ? = 1就能够了 难点:假设每次都是在原来的基础上×2 再推断 ...
- 解决:Determining IP Information for eth0 一直停留 无法进入系统
问题场景: vm centos6.4网卡之前一直没异常,可今天启动时一直卡在Determining IP Information for eth0,无法进入系统.网上说了非常多办法,大多都是不着边的说 ...
- cocoa动态方法决议及消息转发
假设给一个对象发送不能响应的消息,同一时候又没有进行动态方法决议,又没实现消息转发,那么就会引发以下的crash信息 2014-07-30 15:47:54.434 MethodNotFind[171 ...