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,还是比较简单,模拟比赛的时候没认真想,之后推了一下公式,还是比较容易想的。

三维,dp[i][j][k]表示已经占满i行j列,放了k个棋子,还需要放几个棋子到达条件的期望。

则:

dp[i][j][k]=(i*j-k)*1.0/(m*n-k)*dp[i][j][k+1]
+(m*j-i*j)*1.0/(m*n-k)*dp[i+1][j][k+1]
+(n*i-i*j)*1.0/(m*n-k)*dp[i][j+1][k+1]
+(m*n-m*j-n*i+i*j)*1.0/(m*n-k)*dp[i+1][j+1][k+1]+1;

从后向前递推即可;

 #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#define M(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f using namespace std; const int MAXN=;
double dp[MAXN][MAXN][MAXN*MAXN]; int main()
{
int m,n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(int i = max(m,n);i<=m*n;i++)
dp[m][n][i]=;
for(int i=m;i>=;i--)
for(int j=n;j>=;j--)
{
if(i==m&&j==n)continue;
for(int k = i*j;k>=max(i,j);k--)
{
dp[i][j][k]=(i*j-k)*1.0/(m*n-k)*dp[i][j][k+]
+(m*j-i*j)*1.0/(m*n-k)*dp[i+][j][k+]
+(n*i-i*j)*1.0/(m*n-k)*dp[i][j+][k+]
+(m*n-m*j-n*i+i*j)*1.0/(m*n-k)*dp[i+][j+][k+]+;
}
}
//cout<<dp[3][30][90]<<endl;
printf("%.12lf\n",dp[][][]);
}
return ;
}

2014牡丹江D Domination的更多相关文章

  1. zoj 3822 Domination(2014牡丹江区域赛D称号)

    Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headm ...

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

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

  3. zoj 3822 Domination 概率dp 2014牡丹江站D题

    Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headm ...

  4. 2014牡丹江——Domination

    题目链接 题意: 给一个n*m的矩阵,每天随机的在未放棋子的格子上放一个棋子.求每行至少有一个棋子,每列至少有一个棋子的天数的期望  (1 <= N, M <= 50). 分析: 比較明显 ...

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

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

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

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

  7. ZOJ 3829 Known Notation (2014牡丹江H称号)

    主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...

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

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

  9. zoj 3820(2014牡丹江现场赛B题)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5374 思路:题目的意思是求树上的两点,使得树上其余的点到其中一个点的 ...

随机推荐

  1. 常用sql语句整理[SQL Server]

    1. 存储过程 CREATE PROCEDURE [dbo].[bbs_move_createtopic] @fid smallint, @iconid smallint, @curtid INT O ...

  2. Altium Designer 15 --- Design PCB Frame by Rhinoceros

    step 1: Draw a PCB shape and the main component placed in the PCB. The drawing sheet should be in th ...

  3. Yocto开发笔记之《驱动调试-GPS数据采集》(QQ交流群:519230208)

    开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 =============================== ...

  4. Yocto开发笔记之《快速入门,环境搭建 & 编译》(QQ交流群:519230208)

    开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================== ...

  5. 创建 sequence

    -- Create sequence create sequence XRMKSD_DET_SEQminvalue 1maxvalue 999999999999999999999999999start ...

  6. 原创最简单的ORM例子

    这个仅是为了培训做的一个小例子 public class DB     { public static string GetClassName(Type type) { if (type == nul ...

  7. 将一个tabel加到另一个table

    DataTable rate = GetRate(str_catchType,); dt.Merge(rate);//将两个table合并

  8. scrapy3_ 安装指南

    安装指南 安装Scrapy 注解 请先阅读 平台安装指南. 下列的安装步骤假定您已经安装好下列程序: Python 2.7 Python Package: pip and setuptools. 现在 ...

  9. JavaScript学习笔记——变量和数据类型

    一.javascript命名规范 1. 严格区分大小写 2. 变量的命名必须以字母或 _或 $开头,余下的部分可以是任意的字母,数字,或者是 _或者是$ 3.不能用关键字或者是保留字命名. 4.jav ...

  10. typedef和#define的用法与区别

    typedef和#define的用法与区别 typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程 ...