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 chessboardNRows andMColumns.
Every day after work, Edward will place a chess piece on a random empty cell. A few days later, he found the chessboard wasDominatedBy 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 chessboardN×MDominated. Please write a program to help him.
Input
There are multiple test cases. The first line of input contains an integerTIndicating the number of test cases. For each test case:
There are only two integersNAndM(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
21 32 2
Sample output
3.0000000000002.666666666667
Expected idea: calculate the probability first, and then use the expected formula to calculate the number of stones in each row, set DP [I] [J] [k] To show that J exists after I put a stone, and K in the column has the probability of having at least one stone. Then we will discuss the four cases. 1. add 1, 2 to both rows and columns. rows plus 1, 3. add 14. rows and columns are not added with 1
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn = 55;double dp[maxn*maxn][maxn][maxn];int n, m;int main() {int t;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &m);memset(dp, 0, sizeof(dp));dp[1][1][1] = 1.0;for (int i = 1; i < n*m; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= m; k++)if (dp[i][j][k] > 0) {dp[i+1][j+1][k+1] += dp[i][j][k] * (n - j) * (m - k) / (n * m - i);dp[i+1][j+1][k] += dp[i][j][k] * (n - j) * k / (n * m - i);dp[i+1][j][k+1] += dp[i][j][k] * j * (m - k) / (n * m - i);if (j < n || k < m)dp[i+1][j][k] += dp[i][j][k] * (j * k - i) / (n * m - i);}double ans = 0;for (int i = 1; i <= n * m; i++)ans += dp[i][n][m] * i;printf("%.8lf\n", ans);}return 0;}
Zoj-3822 domination (DP)