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. windows的gvim总是报错: +iconv fencview.vim

    iconv是用来转换gvim文件的编码的, 需要插件: iconv.dll gvim7.3的文件目录结构: vim/vim73是它的核心文件, 而vimfiles是扩展文件, 里面的plugin是专门 ...

  2. [SpringBoot] - 发送带附件的邮件

    <!--发送email依赖--> <dependency> <groupId>org.springframework.boot</groupId> &l ...

  3. Qt5.2+opencv2.4.9配置安装过程

    Qt5.2+Opencv2.4.9的安装与配置 安装环境 Win10系统 Qt5.2.0 Opencv2.4.9 1. 安装Qt5.2.0 安装在D:\Qt\5.2.0文件夹(记为A文件夹) Qt下载 ...

  4. 机器学习-数据可视化神器matplotlib学习之路(四)

    今天画一下3D图像,首先的另外引用一个包 from mpl_toolkits.mplot3d import Axes3D,接下来画一个球体,首先来看看球体的参数方程吧 (0≤θ≤2π,0≤φ≤π) 然 ...

  5. hdu 2874 Connections between cities 带权lca判是否联通

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  6. DEV-C++设置C++11标准

    DEV-C++默认的标准是C++98,改成C++11的方法如下: Tools -> Compiler Options -> Setting -> Code Generation -& ...

  7. chrome的url列表里面也找不到chrome://plugins的原因

    plugins 页面被移除后,可以访问: chrome://settings/content 调整 Flash.PDF 的设置. 原本 plugins 页面就基本只剩下这两货了,移除掉确实没啥影响. ...

  8. Unix网络编程_卷1卷2

    1. UNIX 网络编程(第2版)第1卷:套接口API和X/Open 传输接口API PDFhttp://www.linuxidc.com/Linux/2014-04/100155.htm UNIX网 ...

  9. 使用Fragment适应不同屏幕和分辨率

    Fragment是Android3.0后增加的新控件,有点类似于Activity组件,也是用来承载各种View元素.Google增加这个 玩意的目的是为了平板电脑里面可以复用部分显示的View,只要写 ...

  10. linux c/c++ 删除文件

    linux c/c++ 删除文件 #include <stdio.h> int FileRemove(const char* fname) { return remove(fname); ...