Domination Time limit: 8 Seconds Memory Limit: 131072 KB Special Judge
Edward is the headmaster of Marjar University. He is enthusiastic on 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 would place a chess piece on a random empty cell. A few days later, he found the chessboard weredominated by the chess pieces. That means there are 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 x M dominated. Please write a him.
Input
There is multiple test cases. The first line of input contains an integer indicating the number of the T test cases. For each test case:
There is only integers N M and (1 <= N , M <= 50).
Output
For each test case, output the expectation number of days.
Any solution with a relative or absolute error of in most 10-8 would be accepted.
Sample Input
21 32 2
Sample Output
3.0000000000002.666666666667
This problem and POJ2096 very similar, but there is a difference, the number of operation of the problem is limited, (n*m)
Dp[i][j][k] indicates that I have a row J column has a pawn, currently placed K pieces, to reach the target state of the expected
Dp[i][j][k ] = P1 * (dp[i][j][k+1] + 1) + P2 * (dp[i][j + 1][k + 1] + 1) + P3 * (dp[i + 1][j][k + 1] + 1) + P4 * (dp[i + 1][j + 1][k + 1] + 1)
P1: A pawn is currently placed, but the ranks are not changed
I * j is the position of the row and column intersection, only in these positions is possible to meet the requirements, but at the same time these squares are placed on these pieces, to be lost (and the pieces may only be placed in these intersections)
So p1 = (i * j-k)/(n * m-k)
P2: A pawn is currently placed, but the occupied column does not change, the row increases
to achieve this requirement, the position may be J * N-i * J, J * N is the position on all columns, but at the same time these positions Contains the position of the row and column intersection, to reduce the
P2 = (J * N-i * j)/(n * m-k)
The same P3 for the current position of a pawn, but the occupied row does not change, the column increases
P3 = (i * m-i * j)/(n * m -K)
P4: The current position of a pawn, occupy the ranks of the addition of the
I * m + j * N-i * J is the points produced by these pieces of the covered point, in which the point must not reach the P4 requirements, on the contrary, the remaining points can be reached
that n * m-i * M- J * N + i * j these points
P4 = ( n * m-i * m-j * n + i * J )/(n * m-k)
/************************************************************************* > File Name:zoj3822.cpp > Author: ALex > Mail: [email protected] > Created time:2014 December 21 Sunday 12:48 31 seconds ******************************* /#include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int N = 55;double dp[n][n][ N * N];int Main () {int t;scanf ("%d", &t), while (t--) {double P1, p2, p3, P4;int N, m;scanf ("%d%d", &n, &m); Memse T (DP, 0, sizeof (DP)), for (int i = n; I >= 0;-i) {for (int j = m; j >= 0;--j) {if (i = = N && j = = m) {Contin UE;} for (int k = i * j; k >= Max (i, j);--k) {P4 = 1.0 * (n * m-i * m-j * n + i * j)/(n * m-k);p 2 = 1.0 * (J * N-i * j)/(n * m-k);p 3 = 1.0 * (i * m-I * j)/(n * m-k);p 1 = 1.0 * (i * j-k)/(n * m-k);DP [i][j][k] + = P1 * (dp[i][j][k + 1] + 1);DP [i][j][k] + = P2 * (d P[i + 1][j][k + 1] + 1);DP [i][j][k] + = P3 * (dp[i][j + 1][k + 1] + 1);DP [i][j][k] + = P4 * (dp[i + 1][j + 1][k + 1] + 1);} }}printf ("%.12f\n", Dp[0][0][0]);} return 0;}
ZOJ3822----Domination