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

此题和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: [email protected]
    > 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;
}
时间: 2025-01-08 00:19:35

ZOJ3822---- Domination的相关文章

zoj3822 Domination(概率dp)

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 chessboar

ZOJ3822 Domination(14牡丹江 D) 概率DP

题意:给你一个N×M的棋盘,每一次随机在这里放一个子(不能重复)问你最后每一行每一列只要有一个子的期望次数 解题思路:dp[i][j][s] 已经用 i 个子 占了 j 行 s 列的概率,再找出状态转移方程就行. 解题代码: 1 // File Name: d.cpp 2 // Author: darkdream 3 // Created Time: 2014年10月18日 星期六 10时19分16秒 4 5 #include<vector> 6 #include<list> 7

ZOJ3822 ACM-ICPC 2014 亚洲区域赛牡丹江赛区现场赛D题Domination 概率DP(两种解法)

题目地址:点击打开链接 这道题有两种做法,第一种是直接求期望,类似于poj 2096 区别在于这个步数有限.所以要迭代步数. #include <cstdio> #include <cstring> #include <iostream> #define maxn 55//这里刚开始写成了50+10 那么maxn*maxn就会小很多wa了一次 using namespace std; double dp[maxn][maxn][maxn*maxn]; int N,M,T

ZOJ3822——概率DP——Domination

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3822 /* dp[i][j][k]定义为覆盖了i行j列用了>=k个棋子的概率 状态转移方程 dp[i][j][k] = dp[i-1][j][k-1]*1.0*(j*(n-i+1))/(n*m-k+1) + dp[i][j-1][k-1]*1.0*i*(m-j+1)/(n*m-k+1) + dp[i-1][j-1][k-1]*1.0*(n-i+1)*(m-j+1)/(n*

ZOJ 3822 Domination (三维概率DP)

E - Domination Time Limit:8000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit Status Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he

ZOJ-3822

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 chessboar

ZOJ 3822 Domination (概率DP)

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 chessboar

ZOJ 3288 Domination

D - Domination Time Limit:8000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Description 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 larg

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 chessboar

14牡丹江现场赛 D 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 chessboar