2014牡丹江D Domination

Domination

Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge

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 <= N, M <= 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

概率DP,还是比较简单,模拟比赛的时候没认真想,之后推了一下公式,还是比较容易想的。

三维,dp[i][j][k]表示已经占满i行j列,放了k个棋子,还需要放几个棋子到达条件的期望。

则:

dp[i][j][k]=(i*j-k)*1.0/(m*n-k)*dp[i][j][k+1]
+(m*j-i*j)*1.0/(m*n-k)*dp[i+1][j][k+1]
+(n*i-i*j)*1.0/(m*n-k)*dp[i][j+1][k+1]
+(m*n-m*j-n*i+i*j)*1.0/(m*n-k)*dp[i+1][j+1][k+1]+1;

从后向前递推即可;

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<cstring>
 7 #define M(a,b) memset(a,b,sizeof(a))
 8 #define INF 0x3f3f3f3f
 9 
10 using namespace std;
11 
12 const int MAXN=55;
13 double dp[MAXN][MAXN][MAXN*MAXN];
14 
15 int main()
16 {
17     int m,n,t;
18     scanf("%d",&t);
19     while(t--)
20     {
21         scanf("%d%d",&m,&n);
22         for(int i = max(m,n);i<=m*n;i++)
23             dp[m][n][i]=0;
24         for(int i=m;i>=0;i--)
25           for(int j=n;j>=0;j--)
26           {
27               if(i==m&&j==n)continue;
28               for(int k = i*j;k>=max(i,j);k--)
29               {
30                   dp[i][j][k]=(i*j-k)*1.0/(m*n-k)*dp[i][j][k+1]
31                               +(m*j-i*j)*1.0/(m*n-k)*dp[i+1][j][k+1]
32                               +(n*i-i*j)*1.0/(m*n-k)*dp[i][j+1][k+1]
33                               +(m*n-m*j-n*i+i*j)*1.0/(m*n-k)*dp[i+1][j+1][k+1]+1;
34               }
35           }
36           //cout<<dp[3][30][90]<<endl;
37         printf("%.12lf\n",dp[0][0][0]);
38     }
39     return 0;
40 }

 

posted @ 2014-10-14 19:46  haohaooo  阅读(191)  评论(0编辑  收藏  举报