Codeforces146D 概率DP

Bag of mice

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.

Examples

Input
1 3
Output
0.500000000
Input
5 5
Output
0.658730159

题意:一对情侣玩抓老鼠游戏,老鼠有黑白两色,女生为先手,先抓到白老鼠胜。 特别的,若两人均未抓到则算男生赢,且两人抓完后会跑走一只老鼠,还会随机放走一只老鼠,问女生赢的概率是多少。

解析:

设dp[i][j]表示现在轮到女生抓时有i只白鼠,j只黑鼠,女赢的概率
明显 dp[0][j]=0,0<=j<=b;因为没有白色老鼠了
      dp[i][0]=1,1<=i<=w;因为都是白色老鼠,抓一次肯定赢了。
      dp[i][j]可以转化成下列四种状态:
      1、女生抓到一只白鼠,则女赢了,概率为i/(i+j);
      2、女生抓到一只黑鼠,男生抓到一只白色,则女输了,概率为j/(i+j)*i/(i+j-1).
      3、女生抓到一只黑鼠,男生抓到一只黑鼠,跑出来一只黑鼠,则转移到dp[i][j-3]。
      概率为j/(i+j)*(j-1)/(i+j-1)*(j-2)/(i+j-2);
      4、女生抓到一只黑鼠,男生抓到一只黑鼠,跑出来一只白鼠,则转移到dp[i-1][j-2].
      概率为j/(i+j)*(j-1)/(i+j-1)*i/(i+j-2);

      当然后面两种情况要保证合法,即第三种情况要至少3只黑鼠,第四种情况要至少2只白鼠
*/
代码:
 1 #include"bits/stdc++.h"
 2 
 3 #define db double
 4 #define ll long long
 5 #define vl vector<ll>
 6 #define ci(x) scanf("%d",&x)
 7 #define cd(x) scanf("%lf",&x)
 8 #define cl(x) scanf("%lld",&x)
 9 #define pi(x) printf("%d
",x)
10 #define pd(x) printf("%f
",x)
11 #define pl(x) printf("%lld
",x)
12 #define rep(i, n) for(int i=0;i<n;i++)
13 using namespace std;
14 const int N   = 1e6 + 5;
15 const int mod = 1e9 + 7;
16 const int MOD = 998244353;
17 const db  PI  = acos(-1.0);
18 const db  eps = 1e-10;
19 const ll INF = 0x3fffffffffffffff;
20 
21 db dp[1005][1005];
22 int t,n,m;
23 int main()
24 {
25     ci(n),ci(m);
26     for(int i=0;i<=m;i++) dp[0][i]=0;
27     for(int i=1;i<=n;i++) dp[i][0]=1;
28     for(int i=1;i<=n;i++){
29         for(int j=1;j<=m;j++)
30         {
31             dp[i][j]+=(db)i/(i+j);
32             if(j>=2) dp[i][j]+=(db)j/(i+j)*(db)(j-1)/(i+j-1)*(db)i/(i+j-2)*dp[i-1][j-2];
33             if(j>=3) dp[i][j]+=(db)j/(i+j)*(db)(j-1)/(i+j-1)*(db)(j-2)/(i+j-2)*dp[i][j-3];
34         }
35     }
36     printf("%.9f
",dp[n][m]);
37     return 0;
38 }


 
原文地址:https://www.cnblogs.com/mj-liylho/p/9536112.html