http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3822
/* dp[i][j][k]定义为覆盖了i行j列用了>=k个棋子的概率 状态转移方程 dp[i][j][k] = dp[i-1][j][k-1]*1.0*(j*(n-i+1))/(n*m-k+1) + dp[i][j-1][k-1]*1.0*i*(m-j+1)/(n*m-k+1) + dp[i-1][j-1][k-1]*1.0*(n-i+1)*(m-j+1)/(n*m-k+1) + dp[i][j][k-1]*(1.0*i*j-k+1)/(n*m-k+1);//k-1已经被覆盖不能选 n-m-k+1表示k-1空格已经被覆盖,要在剩下的空格里面找,需要把剩下的行列补全 */ /************************************************ * Author :Powatr * Created Time :2015-8-17 21:05:24 * File Name :B.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int MAXN = 50 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; double dp[MAXN][MAXN][MAXN*MAXN]; int main(){ int n, m, T; scanf("%d", &T); while(T--){ scanf("%d%d", &n, &m); dp[0][0][0] = 1; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ for(int k = 1; k <= n*m; k++){ dp[i][j][k] = dp[i-1][j][k-1]*1.0*(j*(n-i+1))/(n*m-k+1) + dp[i][j-1][k-1]*1.0*i*(m-j+1)/(n*m-k+1) + dp[i-1][j-1][k-1]*1.0*(n-i+1)*(m-j+1)/(n*m-k+1) + dp[i][j][k-1]*(1.0*i*j-k+1)/(n*m-k+1); } } } double ans = 0; for(int i = 1; i <= n*m; i++){ ans += (dp[n][m][i] - dp[n][m][i-1])*i; } printf("%.10f ", ans); } return 0; }