Zoj 3822 Domination probability dp 2014 Mudanjiang Station D Question, zoj Mudanjiang
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 chessboardNRows andMColumns.
Every day after work, Edward will place a chess piece on a random empty cell. A few days later, he found the chessboard wasDominatedBy 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 chessboardN×MDominated. Please write a program to help him.
Input
There are multiple test cases. The first line of input contains an integerTIndicating the number of test cases. For each test case:
There are only two integersNAndM(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
21 32 2
Sample Output
3.0000000000002.666666666667
Make sure that each column on the n * m board has the expected pawns.
When the competition was too tight, I started to write the idea of a 2-dimensional dp. After writing it, I realized that the 2-dimensional solution could not describe the probability of each State, then, change it to 3D. The probability formula of slag is wrong again !! It is really anxious. I changed it and changed it. Fortunately, I finally went through the topic of probability dp. The first Asian competition can be circled
Idea: dp [I] [j] [k] indicates that I pawns are used, and j rows exist. If k columns are occupied, four pieces are used:
1. Place it in an existing row or column
2. Place it in an existing row and a new column.
3. Put it in an existing column, in a new row
4. Put it in the new row and column
Initialization: dp [I] [n] [m] = 0; (0 <= I <= n * m)
Dp [0] [0] [0] is the answer.
Set the probability of each state to pi,
Transfer equation: dp [I] [j] [k] = dp [I + 1] [j] [k] * p1 + dp [I + 1] [j + 1] [k] * p2 + dp [I + 1] [j] [k + 1] * p3 + dp [I + 1] [j + 1] [k + 1] * p4 + 1;
Where:
P1 = 1.0 * (j * k-I)/(n * m-I );
P2 = 1.0 * (n-j) * k/(n * m-I );
P3 = 1.0 * (m-k) * j/(n * m-I );
P4 = 1.0 * (n-j) * (m-k)/(n * m-I );
I will not talk about how to push it. It is very simple to unify all the positions on one side, and nothing else. Note that j * k> = I, the expectation of probability dp is hard to solve, but it is not very difficult to solve this type of question.
#include <iostream>#include <cstring>#include <cstdio>using namespace std;double dp[2505][55][55];int main(){ int t,n,m; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); memset(dp,0,sizeof(dp)); for(int i=n*m-1;i>=0;i--){ for(int j=n;j>=0;j--){ for(int k=m;k>=0;k--){ if(j==n&&k==m)continue; if(j*k<i)continue; double p1,p2,p3,p4; p1=1.0*(j*k-i)/(n*m-i); p2=1.0*(n-j)*k/(n*m-i); p3=1.0*(m-k)*j/(n*m-i); p4=1.0*(n-j)*(m-k)/(n*m-i); dp[i][j][k]=dp[i+1][j][k]*p1+dp[i+1][j+1][k]*p2+dp[i+1][j][k+1]*p3+dp[i+1][j+1][k+1]*p4+1; } } } printf("%.10lf\n",dp[0][0][0]); } return 0;}