! Codeforces 399D Painting the wall-probability DP

Source: Internet
Author: User

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, otherwise, each time a random selection of a lattice, if the brush is not brush if not brush, and then rest a minute, to stop working time of the mathematical expectations (before the start has been the M-lattice brush)

Analysis: Probability DP

Status: Dp[i][j] Indicates that there is an I row J column without brush, then it can be transferred to the state of dp[i][j],dp[i-1][j-1],dp[i][j-1],dp[i-1][j-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

Initialization's not quite understood.

Code:

#include <iostream> #include <cstdio>using namespace std;double dp[2010][2010];int n,m,a[2010],b[2010]; int main () {cin>>n>>m;int x,y;int l=n,r=n;for (int i=0;i<m;i++) {cin>>x>>y;if (!a[x]) l--;if (!b[y]) r--;a[x]=1,b[y]=1;} for (int i=1;i<=l;i++) dp[i][0]=dp[i-1][0]+ (double) n/i;for (int j=1;j<=r;j++) dp[0][j]=dp[0][j-1]+ (double) n/j; for (int i=1;i<=l;i++) {for (int j=1;j<=r;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) * (N-J));}} printf ("%0.8lf\n", Dp[l][r]);}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

! Codeforces 399D 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: info-contact@alibabacloud.com 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.