CodeForces 148D Bag of mice

概率,$dp$。

$dp[i][j][0]$表示还剩下$i$个白猫,$j$个黑猫,公主出手的情况下到达目标状态的概率。

$dp[i][j][1]$表示还剩下$i$个白猫,$j$个黑猫,龙出手的情况下到达目标状态的概率。

一开始$dp[i][0][0]$均为$1$,答案为$dp[w][b][0]$。递推式很容易写:

$dp[i][j][0]=i/(i+j)+j/(i+j)*dp[i][j-1][1]$

$dp[i][j][1]=j/(i+j)*i/(i+j-1)*dp[i-1][j-1][0]+j*(j-1)/((i+j)*(i+j-1))*dp[i][j-2][0]$

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\in.txt","r",stdin);
    freopen("D:\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar();
    x = 0;
    while(!isdigit(c)) c = getchar();
    while(isdigit(c))
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
}

double dp[1005][1005][2];
int w,b;

int main()
{
    while(~scanf("%d%d",&w,&b))
    {
        memset(dp,0,sizeof dp);
        for(int i=1;i<=w;i++) dp[i][0][0]=1;
        for(int i=1;i<=w;i++)
        {
            for(int j=1;j<=b;j++)
            {
                dp[i][j][0]=1.0*i/(i+j)+1.0*j/(i+j)*dp[i][j-1][1];
                dp[i][j][1]=1.0*j/(i+j)*i/(i+j-1)*dp[i-1][j-1][0];
                if(j>=2) dp[i][j][1]+=1.0*j*(j-1)/((i+j)*(i+j-1))*dp[i][j-2][0];
            }
        }
        printf("%.9f
",dp[w][b][0]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/6322921.html