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

#include<cstdio>
#include<cstring>
using namespace std;
double dp[60][60][3000];
void printdp(int x,int y){
for(int i=0;i<=x;i++){
for(int j=0;j<=y;j++){
for(int k=0;k<=i*j;k++){
printf("dp[%d][%d][%d]%f ",i,j,k,dp[i][j][k]);
}
}
puts("");
}
}
int main(){
int T;
int n,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
memset(dp[i][j],0,sizeof(dp[i][j]));
}
}
dp[0][0][0]=1;
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
for(int k=0;k<=i*j;k++){
if(k==n*m||(i==n&&j==m)) continue;
if(k<i*j)dp[i][j][k+1]+=dp[i][j][k]*(i*j-k)/(n*m-k);
if(i<n)dp[i+1][j][k+1]+=dp[i][j][k]*(n-i)*j/(n*m-k);
if(j<m)dp[i][j+1][k+1]+=dp[i][j][k]*(m-j)*i/(n*m-k);
if(i<n&&j<m)dp[i+1][j+1][k+1]+=dp[i][j][k]*(n-i)*(m-j)/(n*m-k);
}
}
}
double sum=0;
for(int k=0;k<=n*m;k++){
sum+=k*dp[n][m][k];
// sum+=dp[n][m][k];
// printf("dp[%d]%f\n",k,dp[n][m][k]);
}
//printdp(n,m);
//double ans=(double)sub/(double)sum;
printf("%.12f\n",sum);
}
return 0;
}

  

ZOJ 3822 Domination 概率dp 难度:0的更多相关文章

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

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

  2. zoj 3822 Domination (概率dp 天数期望)

    题目链接 参考博客:http://blog.csdn.net/napoleon_acm/article/details/40020297 题意:给定n*m的空棋盘 每一次在上面选择一个空的位置放置一枚 ...

  3. ZOJ 3822 Domination(概率dp)

    一个n行m列的棋盘,每天可以放一个棋子,问要使得棋盘的每行每列都至少有一个棋子 需要的放棋子天数的期望. dp[i][j][k]表示用了k天棋子共能占领棋盘的i行j列的概率. 他的放置策略是,每放一次 ...

  4. ZOJ 3822 Domination(概率dp 牡丹江现场赛)

    题目链接:problemId=5376">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376 Edward ...

  5. zoj 3822(概率dp)

    ZOJ Problem Set - 3822 Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Ju ...

  6. ZOJ 3822 Domination 期望dp

    Domination Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...

  7. zoj 3822 Domination (可能性DP)

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

  8. ZOJ - 3822 Domination (DP)

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

  9. CF 148D Bag of mice 概率dp 难度:0

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案

    Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...

  2. python_实现发送邮件功能

    #!/usr/bin/env python #-*- coding:utf-8 -*- from email import encoders from email.header import Head ...

  3. HDU 1796 How many integers can you find(容斥)题解

    思路:二进制解决容斥问题,就和昨天做的差不多.但是这里题目给的因子不是质因子,所以我们求多个因子相乘时要算最小公倍数.题目所给的因数为非负数,故可能有0,如果因子为0就要删除. 代码: #includ ...

  4. 如何在一台主机上管理自己的多个git repository

    在使用git时,通常是直接ssh-keygen生成默认秘钥.然后将共钥添加到远程仓库,就可以访问了. 但是,当我们有多个repository时,这种方式就不适用了,因为一个秘钥只能关联一个远程仓库. ...

  5. Open Source Log Management

    https://www.elastic.co/solutions/logging The Elastic Stack (sometimes known as the ELK Stack) is the ...

  6. YOLO(Darknet官方)训练分类器

    目录 1. 分类数据准备 2. Darknet配置 3. Darknet命令使用 4. cifar-10 使用示例 1. 分类数据准备 需要的文件列表: 1. train.list : 训练的图片的绝 ...

  7. Pyinstaller 中 pandas出错问题的解决(详细)

    环境配置 pip install pyinstaller pyinstaller中的参数 -F 表示生成单个可执行文件 -c 显示命令行窗口,一般一开始的时候使用,如果没有错误,那就使用-w命令 -w ...

  8. The way to Go(1): Introduction

    Reference: Github: Go Github: The way to Go Introduction to Go 设计要求: 能够以更快的速度开发软件 开发出的软件能够很好地在现代的多核计 ...

  9. StringBuffer中的sBuffer.delete(0,4);

    只删除第0-3位的字符,第4位是不删的

  10. 环境变量.JAVA_HOME

    1.资料:(我的文章) http://www.cnblogs.com/cppskill/p/8341263.html 2. 2.1.界面 2.2.代码 unit formMain; interface ...