【CF148D】Bag of mice

题目

题意翻译

袋子里有ww 只白鼠和bb 只黑鼠 ,A和B轮流从袋子里抓,谁先抓到白色谁就赢。A每次随机抓一只,B每次随机抓完一只之后会有另一只随机老鼠跑出来。如果两个人都没有抓到白色则B赢。A先抓,问A赢的概率。

输入

一行两个数w,bw,b 。

输出

A赢的概率,误差10^{-9}10
−9
以内。

数据范围

0\le w,b\le 10000≤w,b≤1000 。

题目描述

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 w white and b 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 w and b b ( 0<=w,b<=1000 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} 10
−9
.

输入输出样例

输入样例#1: 复制
1 3
输出样例#1: 复制
0.500000000
输入样例#2: 复制
5 5
输出样例#2: 复制
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.

思路

记忆化搜索

设f[x][y]为黑白鼠各剩多少只A的胜率

当x=0||y=0直接返回
其他的分类讨论

代码

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1077;
int w,b;
double f[maxn][maxn];
double dfs(int x,int y)
{
    if(x==0) return 0.0;
    if(y==0) return 1.0;
    if(f[x][y]>0) return f[x][y];
    double ans=0;
    ans+=1.0*x/(x+y);
    if(y==2)
    ans+=1.0*y/(x+y)*(y-1)/(x+y-1)*dfs(x-1,y-2);
    else if(y>=3)
    ans+=1.0*y/(x+y)*(y-1)/(x+y-1)*
    (1.0*x/(x+y-2)*dfs(x-1,y-2)+1.0*(y-2)/(x+y-2)*dfs(x,y-3));
    return f[x][y]=ans;
}

int main()
{
    scanf("%d%d",&w,&b);
    printf("%.9lf",dfs(w,b));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值