(ZOJ 3822)Domination(概率DP)

Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboard with N rows and M columns.

Every day after work, Edward will place a chess piece on a random empty cell. A few days later, he found the chessboard was dominated by the chess pieces. That means there is at least one chess piece in every row. Also, there is at least one chess piece in every column.

"That's interesting!" Edward said. He wants to know the expectation number of days to make an empty chessboard of N × M dominated. Please write a program to help him.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

There are only two integers N and M (1 <= NM <= 50).

Output

For each test case, output the expectation number of days.

Any solution with a relative or absolute error of at most 10-8 will be accepted.

Sample Input

2
1 3
2 2

Sample Output

3.000000000000
2.666666666667

疯了!!wa了能有10+遍,最后发现是memset(dp,0,sizeof(0));!!!!!!

dp[i][j][k]表示有i行j列满足条件,已经用了k个棋子
每个dp状态可以推导出4个其他状态,具体参见代码~
和其他几道题差不多
#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<string.h>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f

using namespace std;

double dp[60][60][2600];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        double n,m;
        scanf("%lf%lf",&n,&m);
        memset(dp,0,sizeof(dp));
        for(int i=n;i>=0;i--)
            for(int j=m;j>=0;j--)
                for(int k=i*j;k>=max(i,j);k--)
                    {
                        if(i==n&&j==m)
                            continue;
                        double p1=1.0*j*(n-i)/(1.0*n*m-k);
                        double p2=1.0*i*(m-j)/(1.0*n*m-k);
                        double p3=1.0*(i*j-k)/(1.0*n*m-k);
                        double p4=1.0*(n-i)*(m-j)/(1.0*n*m-k);
                            dp[i][j][k]=(p1*dp[i+1][j][k+1]+p2*dp[i][j+1][k+1]+p3*dp[i][j][k+1]+p4*dp[i+1][j+1][k+1]+1.0);
                    }
        printf("%.12lf\n",dp[0][0][0]);
    }
    return 0;
}

 

posted @ 2018-02-08 21:49  海哥天下第一  阅读(142)  评论(0编辑  收藏  举报