codeforces 148 D. Bag of mice

http://codeforces.com/problemset/problem/148/D

题意:盒子里有w只白老鼠,b只黑老鼠,公主和魔王轮流取(公主先),先取到白老鼠的人获胜。魔王每次取完以后,盒子中的老鼠会因为吓尿了跑掉一只,跑掉的老鼠不算任何人取的。问公主获胜的概率。

思路:概率dp.. dp[i][j]表示有i只白老鼠,j只黑老鼠的时候公主获胜的概率。

转移方程

 11. 公主抽到白老鼠(之后龙不必再抽)  胜率为i/(i+j)*1 
 22. 公主抽到黑老鼠,龙抽到黑老鼠,跳出一只黑老鼠,胜率为j/(i+j) * (j-1)/(i+j-1) * (j-2)/(i+j-2) * f[i][j-3] (j>=3)
 33. 公主抽到黑老鼠,龙抽到黑老鼠,跳出一只白老鼠,胜率为j/(i+j) * (j-1)/(i+j-1) * (i/(i+j-2) * f[i-1][j-2] (j>=2)
 44. 龙抽到白老鼠,胜率为0
 5
 6
 7
 8
 9
10
11
12
13
14/* ***********************************************
15Author :111qqz
16Created Time :2016年02月04日 星期四 02时44分23秒
17File Name :code/cf/problem/148D.cpp
18************************************************ */
19
20#include <cstdio>
21#include <cstring>
22#include <iostream>
23#include <algorithm>
24#include <vector>
25#include <queue>
26#include <set>
27#include <map>
28#include <string>
29#include <cmath>
30#include <cstdlib>
31#include <ctime>
32#define fst first
33#define sec second
34#define lson l,m,rt<<1
35#define rson m+1,r,rt<<1|1
36#define ms(a,x) memset(a,x,sizeof(a))
37typedef long long LL;
38#define pi pair < int ,int >
39#define MP make_pair
40
41using namespace std;
42const double eps = 1E-8;
43const int dx4[4]={1,0,0,-1};
44const int dy4[4]={0,-1,1,0};
45const int inf = 0x3f3f3f3f;
46const int N=1E3+7;
47int w,b;
48double dp[N][N];
49int main()
50{
51	#ifndef  ONLINE_JUDGE 
52	freopen("code/in.txt","r",stdin);
53  #endif
54	cin>>w>>b;
55
56	ms(dp,0);
57	//dp[i][j]表示有i个白球,j个黑球的时候公主获胜的概率。
58	for ( int i = 1 ; i <= w ; i++) dp[i][0] = 1;
59	for ( int j = 1 ; j <= b ; j++) dp[0][j]= 0 ; 
60
61	for ( int i = 1 ; i <= w ; i ++)
62	{
63	    for ( int j = 1 ; j <= b ; j++)
64	    {
65		double x = i*1.0;
66		double y = j*1.0;
67
68		dp[i][j]+=x/(x+y);
69		if (j>=3) dp[i][j] +=y/(x+y)*(y-1)/(x+y-1)*(y-2)/(x+y-2)*dp[i][j-3];
70
71		if (j>=2) dp[i][j] +=y/(x+y)*(y-1)/(x+y-1)*(x)/(x+y-2)*dp[i-1][j-2];
72	    }
73	}
74	printf("%.10f\n",dp[w][b]);
75
76  #ifndef ONLINE_JUDGE  
77  fclose(stdin);
78  #endif
79    return 0;
80}