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 exceed 10 - 9.
Example
Input
1 3
Output
0.500000000
Input
5 5
Output
0.658730159
公主和恶龙从袋子里装老鼠,抓到白老鼠的赢,袋子里没白老鼠算魔王赢,每次魔王抓完会有一只老鼠随机跑掉,求公主赢的概率,还是很容易递推概率的,虽然老鼠的数量上限不大,不记忆化的话也是会超时的,多一个二维数组记录出现过的概率就行了。
概率递推就是公主抓到白老鼠的概率+公主抓到黑老鼠的概率 * 魔王抓到黑老鼠的概率 * (跑掉黑老鼠*下一轮(w,b-3)+跑掉白老鼠*下一轮(w-1,b-2))
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<cmath>
#include<stdio.h>
using namespace std;
int w,b;
double v[1005][1005];
double dfs(int white,int black){
if(white==0)return 0.0;//only black
else if(black==0){return 1.0;}//only white
if(v[white][black]!=0)return v[white][black];
double win=0;
double cw=double(white)/double(white+black);
win+=double(white)/double(white+black);//gongzhuchoubai
cw=(1.0-cw)*double(black-1)/double(white+black-1);//mowang chou hei
double cnt=0.0;
if(black>=3){
cnt+=dfs(white,black-3)*(double(black-2)/double(black+white-2));
}
if(white>=1&&black>=2){
cnt+=dfs(white-1,black-2)*(double(white)/double(black+white-2));
}
win+=cw*cnt;
v[white][black]=win;
return win;
}
int main(){
scanf("%d%d",&w,&b);
printf("%.9lf",dfs(w,b));
return 0;
}