E-dominationTime
limit:8000MS
Memory Limit:131072KB
64bit IO Format:%lld &%llu SubmitStatus
Description
Edward is the headmaster of Marjar University. He is enthusiastic on 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 would place a chess piece on a random empty cell. A few days later, he found the chessboard were dominatedby the chess pieces. That means there are 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 x M dominated. Please write a him.
Input
There is multiple test cases. The first line of input contains an integer indicating the number of the T test cases. For each test case:
There is only integers N M and (1 <= N , M <= 50).
Output
For each test case, output the expectation number of days.
Any solution with a relative or absolute error of in most 10-8 would be accepted.
Sample Input
21 32 2
Sample Output
3.0000000000002.666666666667
Test instructions
An n-row m-column of the chessboard, each time you can put a pawn, ask to make the chessboard each row each column has at least one piece need to put the number of pieces of the desired.
So for each piece, on the existing board, it may have four effects: The new one, a new row, both a new row and a new column, no impact.
Note that the no effect here refers not to the next in the same position, which is not allowed, refers to the existing (up), (2,1), under (no impact), no increase in rows and columns.
One of the following:
Dp[i][j][k] Already occupy I row J column, walk the time of k step, still need to walk the expectation of the number of steps.
#include <cstdio>#include<cstring>#include<iostream>using namespacestd;Doubledp[ -][ -][ -* -];intMain () {intT; scanf ("%d",&t); while(t--) { intn,m; scanf ("%d%d",&n,&m); Memset (DP,0,sizeof(DP)); for(inti=n;i>=0; i--) for(intj=m;j>=0; j--) for(intK=i*j;k>=max (i,j); k--) { if(i==n&&j==m)Continue; DP[I][J][K]+=1.0* (n-i) *j/(1.0*N*M-K) *dp[i+1][j][k+1]; DP[I][J][K]+=1.0*i* (M-J)/(1.0*N*M-K) *dp[i][j+1][k+1]; DP[I][J][K]+=1.0* (n-i) * (M-J)/(1.0*N*M-K) *dp[i+1][j+1][k+1]; DP[I][J][K]+=1.0* (I*J-K)/(1.0*N*M-K) *dp[i][j][k+1]; DP[I][J][K]+=1.0; } printf ("%.12lf\n", dp[0][0][0]); }}
Two:
DP[I][J][K] indicates the probability of using K-pieces to occupy the I-row J column of a chessboard.
So using dp[i][j][k]-dp[i][j][k-1] to get is the probability that the K-pieces happen to occupy each row of each column.
#include <cstdio>#include<cstring>Doubledp[ -][ -][2550];intn,m;intMain () {intt,i,j,k; scanf ("%d",&T); while(t--) {scanf ("%d%d",&n,&m); intsum=n*m; for(i=0; i<=n;i++) for(j=0; j<=m;j++) for(k=0; k<=sum;k++) dp[i][j][k]=0; dp[0][0][0]=1.0; for(k=1; k<=sum;k++) for(i=1; i<=n;i++) for(j=1; j<=m;j++) {Dp[i][j][k]+ = (dp[i][j][k-1]* (i*j-k+1)*1.0/(sum-k+1));//the added location has no new rows or new columnsdp[i][j][k]+= (dp[i-1][j][k-1]* ((n-i+1) *j) *1.0/(sum-k+1));//increase rows without adding columnsdp[i][j][k]+= (dp[i][j-1][k-1]* (m-j+1) *i*1.0/(sum-k+1));//increase columns without adding rowsdp[i][j][k]+= (dp[i-1][j-1][k-1]* (n-i+1) * (m-j+1)*1.0/(sum-k+1));//Increase Both the column and row//printf ("i:%d j;%d k;%d dp:%lf\n", I,j,k,dp[i][j][k]); } Doubleans=0; for(k=1; k<=sum;k++) ans+= (dp[n][m][k]-dp[n][m][k-1])*K; printf ("%.10lf\n", ans); } return 0;}
ZOJ 3822 Domination (three-dimensional probability dp)