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 <= 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
#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;
}
ZOJ - 3822 Domination (DP)的更多相关文章
- zoj 3822 Domination(2014牡丹江区域赛D称号)
Domination Time Limit: 8 Seconds Memory Limit: 131072 KB Special Judge Edward is the headm ...
- ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)
Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often ...
- ZOJ 3822 Domination 期望dp
Domination Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...
- ZOJ 3822 Domination(概率dp 牡丹江现场赛)
题目链接:problemId=5376">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376 Edward ...
- ZOJ 3822 Domination(概率dp)
一个n行m列的棋盘,每天可以放一个棋子,问要使得棋盘的每行每列都至少有一个棋子 需要的放棋子天数的期望. dp[i][j][k]表示用了k天棋子共能占领棋盘的i行j列的概率. 他的放置策略是,每放一次 ...
- zoj 3822 Domination (可能性DP)
Domination Time Limit: 8 Seconds Memory Limit: 131072 KB Special Judge Edward is the headm ...
- ZOJ 3822 Domination 概率dp 难度:0
Domination Time Limit: 8 Seconds Memory Limit: 131072 KB Special Judge Edward is the headm ...
- zoj 3822 Domination 概率dp 2014牡丹江站D题
Domination Time Limit: 8 Seconds Memory Limit: 131072 KB Special Judge Edward is the headm ...
- zoj 3822 Domination (概率dp 天数期望)
题目链接 参考博客:http://blog.csdn.net/napoleon_acm/article/details/40020297 题意:给定n*m的空棋盘 每一次在上面选择一个空的位置放置一枚 ...
随机推荐
- 关于Oralce数据库优化的几点总结
个人理解,数据库性能最关键的因素在于IO,因为操作内存是快速的,但是读写磁盘是速度很慢的,优化数据库最关键的问题在于减少磁盘的IO,就个人理解应该分为物理的和逻辑的优化, 物理的是指oracle产品本 ...
- 使用HashMap须要注意的事儿:不要暴露Map.entry给外部不可信代码Map.entrySet()
Map/HashMap是java中一种非经常常使用的数据结构,一般我们在应用中做的事情就是调用put向容器写入数据或者是get从容器读取数据. Map.entrySet()这种方法返回了键值对的集合, ...
- UDP vs. TCP
UDP vs. TCP 原文:UDP vs. TCP,作者是Glenn Fiedler,专注于游戏网络编程相关工作多年. 说在最前面的话 翻译这篇文章的初衷:我在工作中根本接触不到网络游戏编程,但是我 ...
- DS Scheduler 0.7 发布,Linux 调度系统 - 开源中国社区
DS Scheduler 0.7 发布,Linux 调度系统 - 开源中国社区 DS Scheduler 0.7 发布,Linux 调度系统
- Android最方便的数据库--LitePal
郭最近看到神分析LitePal相框,我感觉很强烈,尝试了一下,真的好,我不知道,如果你不习惯学习,那么各不相同,我觉得很合适 看完之后,思想,对于我来说,,实体到set颂值,如果数据非常多,那么你可以 ...
- Factorization Machines 学习笔记(四)学习算法
近期学习了一种叫做 Factorization Machines(简称 FM)的算法.它可对随意的实值向量进行预測.其主要长处包含: 1) 可用于高度稀疏数据场景:2) 具有线性的计算复杂度.本文 ...
- 字典实体类:DictionaryEntry类
DictionaryEntry类是一个字典集合,主要包括的内容是键/值对.这样的组合方式能够方便地定位数据,当中的"键"具备唯一性,类似于数据库中的"id",一 ...
- 使用AndroidFrameworks开发和应用隐藏类 or Android使用自定义framework开发与应用
Android眼下代表系统的开源手机操作系统已经更新到4.0.3版本号.由于其开源特性.使得操作系统本身所具有的最大的灵活性,但同时也引起的版本号的多样性,市场上出现的是手机厂商或ROM.可是怎样开发 ...
- 使用jquery+一般处理程序异步载入信息
需求:有时候.web界面对性能要求比較高.我们就不考虑使用asp.net控件.而是使用html标签+jquery+一般处理程序来进行异步处理. watermark/2/text/aHR0cDovL2J ...
- C++ Preprosessor import
#import Attributes Provides links to attributes used with the #import directive. Microsoft Specific ...