1. 程式人生 > >Bag of mice CodeForces -概率DP

Bag of mice CodeForces -概率DP

  • #include<bits/stdc++.h>
    using namespace std;
    #define maxn 1111
    double dp[2*maxn][maxn];
    int w,b;
    int main()
    {
        scanf("%d%d",&w,&b);
        memset(dp,0.0,sizeof(dp));
        for(int i=1; i<=w; i++)
        {
            dp[i][0]=1.0;
            for(int j=1; j<=b; j++)
            {
                double x=i;
                double y=j;
                //直接獲勝
                dp[i][j]+=x/(x+y);
                //兩個全抓的黑的之後獲勝分兩種情況跑出黑的跑出白的
                if(j>=3)
                    dp[i][j]+=y/(x+y)*(y-1.0)/(x+y-1.0)*(y-2.0)/(x+y-2.0)*dp[i][j-3];
                if(j>=2)
                    dp[i][j]+=y/(x+y)*(y-1.0)/(x+y-1.0)*(x)/(x+y-2.0)*dp[i-1][j-2];
            }
        }
        printf("%.9lf\n",dp[w][b]);
        return 0;
    }