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.
题目大意:
一个袋子里有w个白老鼠和b个黑老鼠。龙和王妃轮流来取,每次抽取一只老鼠,王妃先取。规定先取到白色老鼠的获胜。如果老鼠全部取完还没有人取到白老鼠,那么龙获胜。龙每次去抽取老鼠时,如果袋子里还有其它老鼠,会随机跑出一只,而王妃在抽取时不会发生此种情况。给出w和b问王妃获胜的概率。
一开始第二组样例就过不了,后来看了题解的状态转移方程,和我写的完全一样,后来发现边界条件少写了很多,
这个循环中,对于每个w,b=0和b=1时都没有赋值。其实这个方程也不复杂,还比较好想。
for(int w=1;w<=W;w++)
{
dp[w][0][0]=1;
dp[w][0][1]=0;
dp[w][1][0]= 1.0*w/(w+1) ;
dp[w][1][1]= w==1?0: 1.0/(w+1) ;
for(int b=2;b<=B;b++)
{
dp[w][b][0]=1.0*w/(w+b) +1.0*b/(w+b)*dp[w][b-1][1];
dp[w][b][1]=1.0*b/(w+b) *(1.0*w/(w+b-1)*dp[w-1][b-1][0] +1.0*(b-1)/(w+b-1)*dp[w][b-2][0] );
}
}
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI (4.0*atan(1.0))
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson ind<<1,le,mid
#define rson ind<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk make_pair
#define _f first
#define _s second
using namespace std;
//const int INF= ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxw= 1000+10 ;
//const int maxm= ;
double dp[maxw][maxw][2];
int main()
{
int W,B;
while(~scanf("%d%d",&W,&B))
{
for(int b=0;b<=B;b++)
{
dp[0][b][0]=dp[0][b][1]=0;
}
for(int w=1;w<=W;w++)
{
dp[w][0][0]=1;
dp[w][0][1]=0;
dp[w][1][0]= 1.0*w/(w+1) ;
dp[w][1][1]= w==1?0: 1.0/(w+1) ;
for(int b=2;b<=B;b++)
{
dp[w][b][0]=1.0*w/(w+b) +1.0*b/(w+b)*dp[w][b-1][1];
dp[w][b][1]=1.0*b/(w+b) *(1.0*w/(w+b-1)*dp[w-1][b-1][0] +1.0*(b-1)/(w+b-1)*dp[w][b-2][0] );
}
}
printf("%.9lf\n", dp[W][B][0]);
}
return 0;
}