codeforces 148 D. Bag of mice
http://codeforces.com/problemset/problem/148/D
题意:盒子里有w只白老鼠,b只黑老鼠,公主和魔王轮流取(公主先),先取到白老鼠的人获胜。魔王每次取完以后,盒子中的老鼠会因为吓尿了跑掉一只,跑掉的老鼠不算任何人取的。问公主获胜的概率。
思路:概率dp.. dp[i][j]表示有i只白老鼠,j只黑老鼠的时候公主获胜的概率。
转移方程
1. 公主抽到白老鼠(之后龙不必再抽) 胜率为i/(i+j)*1
2. 公主抽到黑老鼠,龙抽到黑老鼠,跳出一只黑老鼠,胜率为j/(i+j) * (j-1)/(i+j-1) * (j-2)/(i+j-2) * f[i][j-3] (j>=3)
3. 公主抽到黑老鼠,龙抽到黑老鼠,跳出一只白老鼠,胜率为j/(i+j) * (j-1)/(i+j-1) * (i/(i+j-2) * f[i-1][j-2] (j>=2)
4. 龙抽到白老鼠,胜率为0
/* ***********************************************
Author :111qqz
Created Time :2016年02月04日 星期四 02时44分23秒
File Name :code/cf/problem/148D.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=1E3+7;
7int w,b;
8double dp[N][N];
9int main()
10{
11 #ifndef ONLINE_JUDGE
12 freopen("code/in.txt","r",stdin);
13 #endif
14 cin>>w>>b;
1 ms(dp,0);
2 //dp[i][j]表示有i个白球,j个黑球的时候公主获胜的概率。
3 for ( int i = 1 ; i <= w ; i++) dp[i][0] = 1;
4 for ( int j = 1 ; j <= b ; j++) dp[0][j]= 0 ;
1 for ( int i = 1 ; i <= w ; i ++)
2 {
3 for ( int j = 1 ; j <= b ; j++)
4 {
5 double x = i*1.0;
6 double y = j*1.0;
dp[i][j]+=x/(x+y);
if (j>=3) dp[i][j] +=y/(x+y)*(y-1)/(x+y-1)*(y-2)/(x+y-2)*dp[i][j-3];
1 if (j>=2) dp[i][j] +=y/(x+y)*(y-1)/(x+y-1)*(x)/(x+y-2)*dp[i-1][j-2];
2 }
3 }
4 printf("%.10f\n",dp[w][b]);
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}