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][k]其中i表示从n中任意选i行,j表示从n中任意选j列,k表示在选出的i行和j列在满足每行列都有一个的情况下选了k个,数组中存满足的概率。
则每次放一个新的棋子对原来状态的影响只有四种可能:
1. 没有影响。
2. 多出新的一行满足要求。
3. 多出新的一列满足要求。
4. 多出新的一行一列满足要求。
状态转移方程分别是:
1. dp[i][j][k] += dp[i - 1][j][k - 1] * (n - i + 1) * j / (n * m - k + 1)
2. dp[i][j][k] += dp[i][j - 1][k - 1] * (m - j + 1) * i / (n * m - k + 1)
3. dp[i][j][k] += dp[i - 1][j - 1][k - 1] * (n - i + 1) * (m - j + 1) / (n * m - k + 1)
4. dp[i][j][k] += dp[i][j][k - 1] * (i * j - k + 1) / (n * m - k + 1)
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<bitset>
#include<numeric>
#include<vector>
#include<string>
#include<iterator>
#include<cstring>
#include<functional>
#define INF 0x3f3f3f3f
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = 55;
const int mod = 1e9 + 7;
const double pi = 3.14159265358979;
typedef pair<int, int> P;
typedef long long ll;
typedef unsigned long long ull;
double dp[maxn][maxn][maxn*maxn];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, m;
scanf("%d%d", &n, &m);
ms(dp, 0);
dp[1][1][1] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
for (int k = 1; k <= i*j; k++)
{
dp[i][j][k] += dp[i - 1][j][k - 1] * ((double)(n - i + 1)*j / (n*m - k + 1));
dp[i][j][k] += dp[i][j - 1][k - 1] * ((double)(m - j + 1)*i / (n*m - k + 1));
dp[i][j][k] += dp[i - 1][j - 1][k - 1] * ((double)(n - i + 1)*(m - j + 1) / (n*m - k + 1));
dp[i][j][k] += dp[i][j][k - 1] * ((double)(i*j - k + 1) / (n*m - k + 1));
}
}
}
double ans = 0;
for (int i = 1; i <= n*m; i++)
{
ans += i*(dp[n][m][i] - dp[n][m][i - 1]);
}
printf("%.12f
", ans);
}
}