zoj 3822(概率dp)

ZOJ Problem Set - 3822

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

Author: JIANG, Kai

Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest

题意:n*m的棋盘中,每下一个棋子会把棋子所在位置的x,y轴覆盖

每天放一个棋子,问所需天数的期望

dp[k][i][j]表上k天覆盖了i条x轴,j条y轴的概率

dp[k][i][j]=dp[k-1][i-1][j-1]*t1+dp[k-1][i][j-1]*t2+dp[k-1][[i][j-1]*t3+dp[k-1][i][j]*t4;

t1 t2 t3 t4对应的概率

特别注意的是只有当i<n||j<m 的时候 dp[k-1][i][j]才有效

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
const ll mod=1e9+;
double dp[N*N][N][N];
int main(){
int n,m;
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
memset(dp,,sizeof(dp));
dp[][][]=;
for(int k=;k<=n*m;k++){
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
double t1=,t2=,t3=,t4=;
double tt=n*m-k+;
t1=1.0*(n*m-(i-)*m-(j-)*(n-i+))/(tt);
t2=1.0*(n*j-(i-)*j)/(tt);
t3=1.0*(m*i-(j-)*i)/(tt);
t4=1.0*(i*j-k+)/(tt);
dp[k][i][j]+=dp[k-][i-][j-]*t1;
dp[k][i][j]+=dp[k-][i-][j]*t2;
dp[k][i][j]+=dp[k-][i][j-]*t3;
if(i<n||j<m)
dp[k][i][j]+=dp[k-][i][j]*t4;
}
}
}
double ans=;
for(int i=;i<=n*m;i++){
ans=ans+dp[i][n][m]*i;
}
printf("%.11f\n",ans);
}
}
上一篇:LA3027 合作网络-并查集压缩路径


下一篇:ZOJ 3822 Domination(概率dp 牡丹江现场赛)