首页 > 代码库 > ZOJ3822---- Domination

ZOJ3822---- 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 withN 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 wasdominated 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 ofN × 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

此题和POJ2096很像,但又有区别,这道题操作次数有有限的,(n*m)
dp[i][j][k]表示已经有i行j列有棋子,目前放置了k个棋子,到达目标状态的期望值

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:当前放置一个棋子,但是占据的行列不变
i * j 是 行列交叉的位置,只有在这些位置上才有可能达到要求,但同时这些格子上又放置这一些棋子,要减掉(而且棋子只可能放在这些交叉的位置上)
因此 p1 = (i * j - k) / (n * m - k)

p2:当前放置一个棋子,但是占据的列不变,行增加
要想达到这个要求, 位置只可能是j * n - i * j, j * n是所有列上的位置,但同时这些位置上包含了行列交叉的位置,要减掉
p2 = (j * n - i * j) / (n * m - k)

同理p3 为 当前放置一个棋子,但是占据的行不变,列增加
p3 = (i * m - i * j) / (n * m - k)

p4:当前放置一个棋子,占据行列都增加
i * m + j * n - i * j 是 由这些棋子产生的被覆盖的点,在这上面放点一定达不到p4的要求,相反,其余点就可以达到
即 n * m - i * m - j * n + i * j 这些点
p4 = (n * m - i * m - j * n + i * j) / (n * m - k)


/*************************************************************************
    > File Name: zoj3822.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2014年12月21日 星期日 12时48分31秒
 ************************************************************************/

#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);
		memset (dp, 0, sizeof(dp));
		for (int i = n; i >= 0; --i)
		{
			for (int j = m; j >= 0; --j)
			{
				if (i == n && j == m)
				{
					continue;
				}
				for (int k = i * j; k >= max(i, j); --k)
				{
					p4 = 1.0 * (n * m - i * m - j * n + i * j) / (n * m - k);
					p2 = 1.0 * (j * n - i * j) / (n * m - k);
					p3 = 1.0 * (i * m - i * j) / (n * m - k);
					p1 = 1.0 * (i * j - k) / (n * m - k);
					dp[i][j][k] += p1 * (dp[i][j][k + 1] + 1);
					dp[i][j][k] += p2 * (dp[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