zoj 3822 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

题目意思是:
有n*m的矩阵,每一天可以在某个没有放过棋子的地方放置棋子。
要求每一行,和每一列至少都有一枚棋子,求天数的期望
可以用三维的数组进行记录 dp[i][j][s] ,表示的意思是目前放了s个棋子,覆盖了i行,j列
转移方程 : dp[i][j][s]---->dp[i][j][s+1]
             ---->dp[i+1][j][s+1]
             ---->dp[i][j+1][s+1]
             ---->dp[i+1][j+1][s+1];
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<set>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 #include<algorithm>
10 #include<cmath>
11 #include<stdlib.h>
12 using namespace std;
13 double dp[55][55][2510];
14 double eps=0.000000001;
15 int main(){
16    int n,m,t;cin>>t;
17    while(t--){
18         cin>>n>>m;
19         memset(dp,0,sizeof(dp));
20         for(int i=n;i>=0;i--)
21         for(int j=m;j>=0;j--){
22             if(i==n&&j==m) continue;
23             int mm=max(i,j);
24             for(int s=i*j;s>=mm;s--){    //对于覆盖i行j列的棋子数最多是 i*j枚,至少是max(i,j);
25                 double p1=0,p2=0,p3=0,p4=0;
26                  //dp[i+1][j][s+1];
27                 p1=(j*(n-i))*1.0/(n*m-s);
28                 //dp[i][j+1][s+1];
29                 p2=((i*(m-j))*1.0)/(n*m-s);
30                 //dp[i+1][j+1][s+1]
31                 p3=(((n-i)*(m-j))*1.0)/(n*m-s);
32                 //dp[i][j][s+1]
33                 p4=1-p1-p2-p3;
36                 dp[i][j][s]=p1*dp[i+1][j][s+1]+p2*dp[i][j+1][s+1]+p3*dp[i+1][j+1][s+1]+p4*dp[i][j][s+1]+1.0;
37             }
38         }
39         printf("%.10f
",dp[0][0][0]);
40    }
41 }
原文地址:https://www.cnblogs.com/ainixu1314/p/4027071.html