///dp[i][j][k]代表i行j列件,并把一k的概率 ///dp[i][j][k]一种常见的方法有四种传输 ///1:dp[i-1][j][k-1] 可能 (n-(i-1))*j/(n*m-(k-1)) ///2:dp[i][j-1][k-1] 概率为 i*(m-(j-1))/(n*m-(k-1)) ///3:dp[i-1][j-1][k-1] 概率为 (n-(i-1))*(m-(j-1))/(n*m-(k-1)) ///4:dp[i][j][k-1] 概率为 (i*j-(k-1))/(n*m-(k-1)) # include <stdio.h> # include <algorithm> # include <string.h> # include <iostream> using namespace std; double dp[55][55][2510]; int main() { int n,m,t,i,j,k; double ans; while(~scanf("%d",&t)) { while(t--) { scanf("%d%d",&n,&m); memset(dp,0,sizeof(dp)); dp[0][0][0]=1; for(i=1; i<=n; i++) { for(j=1; j<=m; j++) { for(k=1; k<=n*m; k++) { if(i==n&&j==m) dp[i][j][k]=dp[i-1][j][k-1]*(n-(i-1))*j/(n*m-(k-1))+dp[i][j-1][k-1]*i*(m-(j-1))/(n*m-(k-1))+dp[i-1][j-1][k-1]*(n-(i-1))*(m-(j-1))/(n*m-(k-1)); else dp[i][j][k]=dp[i-1][j][k-1]*(n-(i-1))*j/(n*m-(k-1))+dp[i][j-1][k-1]*i*(m-(j-1))/(n*m-(k-1))+dp[i-1][j-1][k-1]*(n-(i-1))*(m-(j-1))/(n*m-(k-1))+dp[i][j][k-1]*(i*j-(k-1))/(n*m-(k-1)); } } } ans=0; for(i=1; i<=n*m; i++) ///求期望==概率乘天数的和集 ans+=dp[n][m][i]*i; printf("%.12lf ",ans); } } return 0; }
版权声明:本文博主原创文章,博客,未经同意不得转载。