ZOJ - 3822 Domination (DP)

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

题意:求放石子使得每行没列都有石子个数的期望

思路:先求概率,然后再用期望公式计算,设dp[i][j][k]表示放i个石子后行有j,列有k至少有一个石子的概率,然后就是4种情况的讨论,1.使得行和列都加1,2.行加1,3.列加1

4.行和列都不加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;
}
时间: 2024-12-28 17:10:43

ZOJ - 3822 Domination (DP)的相关文章

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

ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)

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 large decorative chessboard with N rows and M columns. Every day after work, Edward will place

zoj 3822 Domination(2014牡丹江区域赛D题)

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 2014牡丹江站D题

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 3822 Domination 概率DP

题意:在一个n*m的棋盘上放棋子,一个棋子可覆盖一行一列,若n行m列全被覆盖则停止放棋子,求棋子的期望 思路:期望DP, dp[i][j][k]表示放了i个棋子覆盖了j行k列 已知dp[0][0][0]=1,求dp[1~n*m][n][m] 四种情况: 1.再放一个棋子,行列都不增加 dp[i+1][j][k]+=dp[i][j][k]*(j*k-i)*1.0/(m*n-i); 2.只增加一行 dp[i+1][j+1][k]+=dp[i][j][k]*(n-j)*k*1.0/(m*n-i); 3

ZOJ 1743 Concert Hall Scheduling(DP)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=743 题意:有两个音乐厅出租.给出n个租客,每个租客有个租的时间段[L,R],以及租费.任意时候音乐厅只能租给最多一个租客.问如何选择租给哪些租客使得赚的钱最多? 思路:f[i][j]表示第一个音乐厅到时刻i.第二个到时刻j,可以获得的最大值. struct node { int x,y,w; int operator<(const node &a) const

ZOJ 1642 Match for Bonus(dp)

Match for Bonus Time Limit: 2 Seconds      Memory Limit: 65536 KB Roy played a game with his roommates the other day. His roommates wrote 2 strings of characters, and gave each character a bonus value. When Roy pinned the positions of one common char

hdu 5623 KK&#39;s Number(dp)

问题描述 我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1≤N≤5∗10?4??)个数,每次KK都会先拿数.每次可以拿任意多个数,直到NN个数被拿完.每次获得的得分为取的数中的最小值,KK和对手的策略都是尽可能使得自己的得分减去对手的得分更大.在这样的情况下,最终KK的得分减去对手的得分会是多少? 输入描述 第一行一个数T\left( 1\leq T\leq 10\right)T(1≤T≤10),表示数据组