http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3822
题意:n*m的矩阵中,每一个格子都可以涂色,当每一行每一列都有某个格子涂色后,停止涂色。问最后每行每列都至少有一个格子涂色的期望是多少。
题解:dp[i][j][k]表示 前 i 行 前 j 列 放了 k 个的概率
状态转移方程①dp[i][j][k+1] += dp[i][j][k]*(1.0*(i*j-k)/(n*m-k)); ((i*j-k)/(n*m-k)表示在前 i 行前 j 列 已涂了k个还剩下(i*j-k)个格子可以涂,在这块区域内涂色的概率,以下类似)
②dp[i+1][j][k+1] += dp[i][j][k]*(1.0*(n-i)*1.0*j/(n*m-k));
③dp[i][j+1][k+1] += dp[i][j][k]*(1.0*(m-j)*1.0*i/(n*m-k));
④dp[i+1][j+1][k+1] += dp[i][j][k]*(1.0*((n-i)*(m-j))/(n*m-k));
求出概率后,遍历 for(int i = 1; i <= n*m; i++)
ans += dp[n][m][i]*i; 求出期望 (期望等于概率*方案数之和)
/***
* .,, .,:;;iiiiiiiii;;:,,. .,,
* rGB##HS,.;iirrrrriiiiiiiiiirrrrri;,s&##MAS,
* r5s;:r3AH5iiiii;;;;;;;;;;;;;;;;iiirXHGSsiih1,
* .;i;;s91;;;;;;::::::::::::;;;;iS5;;;ii:
* :rsriii;;r::::::::::::::::::::::;;,;;iiirsi,
* .,iri;;::::;;;;;;::,,,,,,,,,,,,,..,,;;;;;;;;iiri,,.
* ,9BM&,WA了活该 .,:;;:,,,,,,,,,,,hXA8: T了天命..,,,.
* ,;&@@#r:;;;;;::::,,. ,r,,,,,,,,,,iA@@@s,,:::;;;::,,. .;.
* :ih1iii;;;;;::::;;;;;;;:,,,,,,,,,,;i55r;;;;;;;;;iiirrrr,..
* .ir;;iiiiiiiiii;;;;::::::,,,,,,,:::::,,:;;;iiiiiiiiiiiiri
* iriiiiiiiiiiiiiiii;;;::::::::::::::::;;;iiiiiiiiiiiiiiiir;
* ,riii;;;;;;;;;;;;;:::::::::::::::::::::::;;;;;;;;;;;;;;iiir.
* iri;;;::::,,,,,,,,,,:::::::::::::::::::::::::,::,,::::;;iir:
* .rii;;::::,,,,,,,,,,,,:::::::::::::::::,,,,,,,,,,,,,::::;;iri
* ,rii;;;::,,,,,,,,,,,,,:::::::::::,:::::,,,,,,,,,,,,,:::;;;iir.
* ,rii;;i::,,,,,,,,,,,,,:::::::::::::::::,,,,,,,,,,,,,,::i;;iir.
* ,rii;;r::,,,,,,,,,,,,,:,:::::,:,:::::::,,,,,,,,,,,,,::;r;;iir.
* .rii;;rr,:,,,,,,,,,,,,,,:::::::::::::::,,,,,,,,,,,,,:,si;;iri
* ;rii;:1i,,,,,,,,,,,,,,,,,,:::::::::,,,,,,,,,,,,,,,:,ss:;iir:
* .rii;;;5r,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,sh:;;iri
* ;rii;:;51,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.:hh:;;iir,
* irii;::hSr,.,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.,sSs:;;iir:
* irii;;:iSSs:.,,,,,,,,,,,,,,,,,,,,,,,,,,,..:135;:;;iir:
* ;rii;;:,r535r:...,,,,,,,,,,,,,,,,,,..,;sS35i,;;iirr:
* :rrii;;:,;1S3Shs;:,............,:is533Ss:,;;;iiri,
* .;rrii;;;:,;rhS393S55hh11hh5S3393Shr:,:;;;iirr:
* .;rriii;;;::,:;is1h555555h1si;:,::;;;iirri:.
* .:irrrii;;;;;:::,,,,,,,,:::;;;;iiirrr;,
* .:irrrriiiiii;;;;;;;;iiiiiirrrr;,.
* .,:;iirrrrrrrrrrrrrrrrri;:.
* ..,:::;;;;:::,,.
*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a));
#define lowbit(x) x&-x;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int maxn = 1e5+5;
const int mod = 1e9+7;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
double dp[55][55][2550];
int n,m;
int main() {
int t;
cin>>t;
while(t--){
scanf("%d%d",&n,&m);
mem(dp,0);
dp[1][1][1] = 1.0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
for(int k = 1; k <= n*m; k++){
if(i == n && j == m) break;
dp[i][j][k+1] += dp[i][j][k]*(1.0*(i*j-k)/(n*m-k));
dp[i+1][j][k+1] += dp[i][j][k]*(1.0*(n-i)*1.0*j/(n*m-k));
dp[i+1][j+1][k+1] += dp[i][j][k]*(1.0*((n-i)*(m-j))/(n*m-k));
dp[i][j+1][k+1] += dp[i][j][k]*(1.0*(m-j)*1.0*i/(n*m-k));
}
}
}
double ans = 0;
for(int i = 1; i <= n*m; i++)
ans += dp[n][m][i]*i;
printf("%.9lf\n",ans);
}
}