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 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
概率dp,i为第i天,j为有j行已经被覆盖。k为k列已经被覆盖。则每一个dp[i][j][k]都能够由上dp[i-1]推出。
代码:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
double dp[5000][55][55];
int main(){
int m,n;
int T;
scanf("%d",&T);
memset(dp, 0, sizeof(dp));
dp[0][0][0]=1; while (T--) {
scanf("%d %d",&m,&n);
for(int i=1;i<=m*n+1-min(m, n);++i){
for(int j=1;j<=i&&j<=m;++j){
for(int k=1;k<=i&&k<=n;++k){
if(j>m){
dp[i][j][k]=0;
continue;
}
if(k>n) {
dp[i][j][k]=0;
continue;
}
if(j!=m||k!=n)
dp[i][j][k]=dp[i-1][j-1][k-1]*(double)((m+1-j)*(n+1-k))/(double)(m*n-i+1)+dp[i-1][j-1][k]*(double)(k*(m+1-j))/(double)(m*n-i+1)+dp[i-1][j][k-1]*(double)(j*(n+1-k))/(double)(m*n-i+1)+dp[i-1][j][k]*(double)(j*k+1-i)/(double)(m*n-i+1);
else
dp[i][j][k]=dp[i-1][j-1][k-1]*(double)((m+1-j)*(n+1-k))/(double)(m*n-i+1)+dp[i-1][j-1][k]*(double)(k*(m+1-j))/(double)(m*n-i+1)+dp[i-1][j][k-1]*(double)(j*(n+1-k))/(double)(m*n-i+1);
}
}
}
double result=0;
for(int i=1;i<=m*n+1-min(m, n);++i){
result+=i*dp[i][m][n];
}
printf("%.12f\n",result);
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
zoj 3822 Domination(2014牡丹江区域赛D称号)的更多相关文章
- zoj 3822 Domination(2014牡丹江区域赛D题) (概率dp)
3799567 2014-10-14 10:13:59 Acce ...
- ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)
Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often ...
- ZOJ 3822 ( 2014牡丹江区域赛D题) (概率dp)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376 题意:每天往n*m的棋盘上放一颗棋子,求多少天能将棋盘的每行每列都至少有 ...
- ZOJ 3827 Information Entropy (2014牡丹江区域赛)
题目链接:ZOJ 3827 Information Entropy 依据题目的公式算吧,那个极限是0 AC代码: #include <stdio.h> #include <strin ...
- zoj 3829 Known Notation(2014在牡丹江区域赛k称号)
Known Notation Time Limit: 2 Seconds Memory Limit: 131072 KB Do you know reverse Polish notatio ...
- ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)
Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...
- The 2014 ACM-ICPC Asia Mudanjiang Regional Contest(2014牡丹江区域赛)
The 2014 ACM-ICPC Asia Mudanjiang Regional Contest 题目链接 没去现场.做的网络同步赛.感觉还能够,搞了6题 A:这是签到题,对于A堆除掉.假设没剩余 ...
- 2014 牡丹江区域赛 B D I
http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=358 The 2014 ACM-ICPC Asia Mudanj ...
- 2014牡丹江区域赛H(特里)ZOJ3826
Hierarchical Notation Time Limit: 2 Seconds Memory Limit: 131072 KB In Marjar University, stude ...
随机推荐
- 4种Delphi IDE的调试时查看内存的方法,太酷了!
1.ctrl+alt+m,可以查看每个函数过程的内存位置 2.Ctrl+Alt+C 查看代码对应的汇编 3.原来用delphi看变量信息一直是简单的用watch看,但是有时候变量值直接用特定类型看总是 ...
- 【Demo 0001】Java基础-数据类型
本章学习要点: 1. 了解Java 语言 2. 了解Java程序结构; 3. 了解Java中基本数据类型; 4. 掌握基本数据类型之间的运算 ...
- linux运维常用命令集
1.删除0字节文件 find -type f -size 0 -exec rm -rf {} \; 2.查看进程 按内存从大到小排列 PS -e -o "%C : %p : %z ...
- c++(重载、覆盖、隐藏)
源地址:http://www.cnblogs.com/qlee/archive/2011/07/04/2097055.html 成员函数的重载.覆盖与隐藏成员函数的重载.覆盖(override)与隐藏 ...
- HDU 1535 Invitation Cards(SPFA,及其优化)
题意: 有编号1-P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费. 有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1. ...
- Android服务之PackageManagerService启动源码分析
了解了Android系统的启动过程的读者应该知道,Android的所有Java服务都是通过SystemServer进程启动的,并且驻留在SystemServer进程中.SystemServer进程在启 ...
- liGDX life_cycle (生命周期)
本文章翻译自libGDX官方wiki,,转载请注明出处:http://blog.csdn.net/kent_todo/article/details/37940489 libGDX官方网址:http: ...
- SmartDraw2008破解过程总结
SmartDraw2008破解过程总结作者:chszs 原创转载请保留作者名. 按下列步骤完毕,保证能够支持中文. 一.所需软件:1)SmartDraw2008安装软件:2)SmartDraw200 ...
- 模拟spring框架注入实现原理
这个我是参见了别人的一些东西,不是原创! 定义一些抽象的方法: package com.huxin.springinject.dao; public interface Person { public ...
- 对付"反盗链"
对付"反盗链" 某些站点有所谓的反盗链设置,其实说穿了很简单, 就是检查你发送请求的header里面,referer站点是不是他自己, 所以我们只需要像把headers的refer ...