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 ...
随机推荐
- Swift - 使用NSUserDefaults来进行本地数据存储
NSUserDefaults适合存储轻量级的本地客户端数据,比如记住密码功能,要保存一个系统的用户名.密码.使用NSUserDefaults是首选.下次再登陆的时候就可以直接从NSUserDefaul ...
- expect实现ssh自动登录
expect实现ssh自动登录 #!/usr/local/bin/expect set PASSWD [lindex $argv 1] set IP [lindex $argv 0] set CM ...
- Android服务之PackageManagerService启动源码分析
了解了Android系统的启动过程的读者应该知道,Android的所有Java服务都是通过SystemServer进程启动的,并且驻留在SystemServer进程中.SystemServer进程在启 ...
- KMP算法(具体求串的next[n])
怎么求串的模式值next[n] )next[0]= -1 意义:不论什么串的第一个字符的模式值规定为-1. )next[j]= -1 意义:模式串T中下标为j的字符,假设与首字符 同样,且 ...
- Gradle学习之使用java plugin
请通过下面方式下载本系列文章的Github演示样例代码: git clone https://github.com/davenkin/gradle-learning.git Gradle最 ...
- table明明设置了固定值
IE真的快把我搞死了 0.0 可爱的迷人的让人醉了的IE你真棒 今天用表格写一个4列的表格 要实现的效果大概是这种 确有用普通浏览器都没有问题 非常easy明了的一个表格嘛!用IE8下面的看就成这样子 ...
- 并发编程实践五:ReentrantLock
ReentrantLock是一个可重入的相互排斥锁,实现了接口Lock,和synchronized相比,它们提供了同样的功能.但ReentrantLock使用更灵活.功能更强大,也更复杂.这篇文章将为 ...
- XML序列化反序列化—常用类
public class XMLSerializer { #region (public) xml序列化 /// <summary> /// ...
- VS2010-win32下cocos2dx控制台打印的方法
在xcode中 直接使用printf 或者 cout<<""<<endl;可以直接在控制台打印 但是在VS2010 却死活不好用 真郁闷 ------ ...
- Juniper srx防火墙NAT配置
一.基础操作说明: 1. 设备恢复出厂化 root# load factory-default root# set system root-authentication plain-text-pas ...