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 <= 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为第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称号)的更多相关文章

  1. zoj 3822 Domination(2014牡丹江区域赛D题) (概率dp)

    3799567 2014-10-14 10:13:59                                                                     Acce ...

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

    Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often ...

  3. ZOJ 3822 ( 2014牡丹江区域赛D题) (概率dp)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376 题意:每天往n*m的棋盘上放一颗棋子,求多少天能将棋盘的每行每列都至少有 ...

  4. ZOJ 3827 Information Entropy (2014牡丹江区域赛)

    题目链接:ZOJ 3827 Information Entropy 依据题目的公式算吧,那个极限是0 AC代码: #include <stdio.h> #include <strin ...

  5. zoj 3829 Known Notation(2014在牡丹江区域赛k称号)

    Known Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB Do you know reverse Polish notatio ...

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

  7. The 2014 ACM-ICPC Asia Mudanjiang Regional Contest(2014牡丹江区域赛)

    The 2014 ACM-ICPC Asia Mudanjiang Regional Contest 题目链接 没去现场.做的网络同步赛.感觉还能够,搞了6题 A:这是签到题,对于A堆除掉.假设没剩余 ...

  8. 2014 牡丹江区域赛 B D I

    http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=358 The 2014 ACM-ICPC Asia Mudanj ...

  9. 2014牡丹江区域赛H(特里)ZOJ3826

    Hierarchical Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB In Marjar University, stude ...

随机推荐

  1. hdu 4704 同余定理+普通快速幂

    此题往后推几步就可找到规律,从1开始,答案分别是1,2,4,8,16.... 这样就可以知道,题目的目的是求2^n%Mod的结果.....此时想,应该会想到快速幂...然后接着会发现,由于n的值过大, ...

  2. 介绍SAP预留函数创建搜索帮助

    紧接上一节介绍的SAP预定义的出口函数F4IF_SHLP_EXIT_EXAMPLE创建搜索帮助, 该函数主要有两个部分: Changing接口的参数属性如下: SHLP:搜索帮助的基础描述,包括搜索帮 ...

  3. “ASP.default_aspx”并不包括“DropDownList1_SelectedIndexChanged”的定义,其解决方法。

    "ASP.default_aspx"并不包括"DropDownList1_SelectedIndexChanged"的定义,其解决方法. 在使用DropDown ...

  4. 百度搜索结果页url参数详解

    在百度首页输入任意关键词搜索之后,我们跳转到搜索结果页面,在浏览器的网址栏我们可以看到很长的一串url地址.那么,你真的了解这一串url的含义吗? s?:搜索 百度搜索结果页使用了重定向,因此我们看到 ...

  5. 更改windows系统语言,改为英文

  6. linux时间方面的设置

    例如以下一段代码能够借鉴: static void _sleep_response_timeout(modbus_t *ctx) { #ifdef _WIN32 /* usleep doesn't e ...

  7. Android实现位图剪切

    我们不能总是依赖于BitmapFactory 以下告诉大家怎么从Bitmaqp中截取某一部分创建新的Bitmap  系统会有一个默认png图片:icon.png 可是这个图片中最外层会有白色的 比較讨 ...

  8. Oschat IM 开源即时通讯项目介绍 - FengJ的个人页面 - 开源中国社区

    Oschat IM 开源即时通讯项目介绍 - FengJ的个人页面 - 开源中国社区 Oschat IM 开源即时通讯项目介绍    255人收藏此文章, 我要收藏 发表于5天前(2013-08-28 ...

  9. Nginx分时段限制下载速度解决方案(原创)_于堡舰_新浪博客

    Nginx分时段限制下载速度解决方案(原创)_于堡舰_新浪博客 Nginx分时段限制下载速度解决方案(原创)    (2011-03-02 16:40:49)    转载▼    标签:    ngi ...

  10. hdu 1063 Exponentiation 大数

    Problem Description Problems involving the computation of exact values of very large magnitude and p ...