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.666666666667题意: 说有个傻孩子喜欢痴迷下象棋,并且有一个习惯,每一天随机放一只棋子在(n*m)的棋盘的随意一个位子里。问当满足任意一行以及任意一列都有棋子时。 再不放了。问这样随机放棋子直到放不了棋子平均需要多少天..... 用DP【i】【j】【k】表示前i行有棋子,前j列有棋子,并且在放了k个棋子的情况下 的概率,对于DP【i】【j】【k】此刻的状态等于上一状态的和...... 推了很久也没有退出这个转移方程..看了一下解题报告,才发下逗比的忘记算dp[i-1][j-1][k-1]这一种情况...代码:
1 //#define LOCAL 2 #include<cstdio> 3 #include<cstring> 4 #define maxn 55 5 double dp[maxn][maxn][maxn*maxn]; 6 int n,m; 7 int main() 8 { 9 #ifdef LOCAL 10 freopen("test.in","r",stdin); 11 #endif 12 int cas,i,j,k; 13 scanf("%d",&cas); 14 while(cas--) 15 { 16 scanf("%d%d",&n,&m); 17 int tt=n*m; 18 memset(dp,0,sizeof(dp)); 19 dp[0][0][0]=1; 20 for(i=1;i<=n;i++) 21 { 22 //表示前i行有旗子 23 for(j=1;j<=m;j++) 24 { 25 //表示前j列有棋子 26 for(k=1;k<=tt;k++) 27 { 28 //当n行m列中都有棋子时 29 if(i==n&&j==m) 30 dp[i][j][k]=(dp[i-1][j][k-1]*(n-i+1)*j+dp[i][j-1][k-1]*(m-j+1)*i+dp[i-1][j-1][k-1]*(n-i+1)*(m-j+1))/(tt-k+1); 31 else 32 dp[i][j][k]=(dp[i-1][j][k-1]*(n-i+1)*j+dp[i][j-1][k-1]*(m-j+1)*i+dp[i-1][j-1][k-1]*(n-i+1)*(m-j+1)+dp[i][j][k-1]*(i*j-k+1))/(tt-k+1); 33 } 34 } 35 } 36 double ans=0; 37 for(int i=0;i<=n*m;i++) 38 ans+=dp[n][m][i]*i; //得到期望 39 printf("%.8lf\n",ans); 40 } 41 return 0; 42 }