codeforces105d Bag of mice ——概率DP

August 18, 2013

Link:

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

Refer to:

http://www.cnblogs.com/kuangbin/archive/2012/10/04/2711184.html

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
using namespace std;
double a[1111][1111];
int main(void)
{
    int w,b;
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    while (~scanf("%d%d",&w,&b))
    {
        for(int i=0;i<=w;++i)for(int j=0;j<=b;++j)a[i][j]=0;
        for(int i=0;i<=b;++i) a[0][i]=0;
        for(int j=1;j<=w;++j) a[j][0]=1;
        for(int i=1;i<=w;++i)
        {
            for(int j=1;j<=b;++j)
            {
                a[i][j]+=(double)i/(i+j);
                if(j>=3)
                    a[i][j]+=a[i][j-3]*(double)j/(i+j)
                    *(double)(j-1)/(i+j-1)
                    *(double)(j-2)/(i+j-2);
                if(i>=2)
                    a[i][j]+=a[i-1][j-2]*(double)j/(i+j)
                    *(double)(j-1)/(i+j-1)
                    *(double)(i)/(i+j-2);
            }
        }
        printf("%.9lf\n",a[w][b]);
    }
    return 0;
}

这题很有意思。为什么自己想不出来?

comments powered by Disqus