Codeforces Round #233 (Div. 2) D. Painting the Wall probability DP

Source: Internet
Author: User

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.

    1. 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.
    2. User Ainta Choose any tile in the wall with uniform probability.
    3. If The tile he has chosen are not painted, he paints the tile. Otherwise, he ignores it.
    4. 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.


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, cin)-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.


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
2 2
1 1
1 2
1 1
1 1

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;}

Codeforces Round #233 (Div. 2) D. Painting the Wall probability DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.