D. Painting the Wall
User Ainta decided to paint a wall. The wall consists of n2 tiles, that is arranged in an n x n table. Some tiles is painted, and the others is not. As he wants to paint it beautifully, he'll follow the rules below.
- Firstly user Ainta looks at the wall. If there is at the least one painted cell on each row and at least one painted cell in each column, he stops coloring. Otherwise, he goes to step 2.
- User Ainta Choose any tile in the wall with uniform probability.
- If The tile he has chosen are not painted, he paints the tile. Otherwise, he ignores it.
- Then he takes a rest for one minute even if he doesn ' t paint the tile. And then Ainta goes to step 1.
However Ainta is worried if it would take too much time to finish this work. So he wants to calculate the expected time needed to paint the wall by the method above. Help him find the expected time. You can assume this choosing and painting any tiles consumes no time at all.
Input
The first line contains Integers n and m (1≤ n ≤2 103; 0≤ m ≤ min ( n 2, 2 104)"-the size of the wall and the number of painted cells.
Next m lines goes, each contains, integers ri and Ci (1≤ ri, ci ≤ n)-the position of the painted cell. It is guaranteed, the positions is all distinct. Consider the rows of the table is numbered from 1 to n. Consider the columns of the table is numbered from1 to n.
Output
In a single line print the expected time-to-paint the wall in minutes. Your answer would be considered correct if it had at most -4 absolute or relative error.
Sample Test (s)
input
5 2
2 3
4 1
Output
11.7669491886
input
2 2
1 1
1 2
Output
2.0000000000
input
1 1
1 1
Output
0.0000000000
Test Instructions : There is a n*n wall, now Xiao Ming to brush the wall, if each row of each column has at least one check brush over the stop work, or each time a random selection of a lattice, if the brush is not brush if not brush, and then rest a minute, The mathematical expectation of the time to stop working (the M-lattice brushes have been brushed before the start)
Dp[i][j] indicates that I row J column is not brushed
Dp[i][0]= ((n-i)/n) *dp[i][0]+dp[i-1][0]*i/n+1;
Dp[0][j]= ((n-j)/n) *dp[0][j]+dp[0][j-1]*j/n+1;
Transfer: dp[i][j]=dp[i][j]* (n-i) (n-j)/n^2+dp[i-1][j]* (i* (n-j))/n^2+dp[i][j-1]* ((n-i) *j)/n^2+dp[i-1][j-1]* (i *J)/n^2+1;
#include <iostream>#include<cstdio>using namespacestd;Doubledp[ .][ .];intn,m,a[ .],b[ .];intMain () {CIN>>n>>m; intx, y; intL=n,r=N; for(intI=0; i<m; i++) {cin>>x>>y; if(!a[x]) l--; if(!b[y]) r--; A[X]=1, b[y]=1; } for(intI=1; i<=n; i++) dp[i][0]=dp[i-1][0]+(Double) n/i; for(intj=1; j<=n; J + +) dp[0][j]=dp[0][j-1]+(Double) n/J; for(intI=1; i<=n; i++) { for(intj=1; j<=n; J + +) {Dp[i][j]= (dp[i-1][j]*i* (N-J) +n*n+dp[i][j-1]*j* (n-i) +dp[i-1][j-1]*I*J)/(n*n-(n-i) * (nj)); }} printf ("%0.10f\n", Dp[l][r]); return 0;}
Code
Codeforces Round #233 (Div. 2) D. Painting the Wall probability DP