Zoj -- 3822 -- probability DP of the second question

Source: Internet
Author: User

This question is more difficult than the previous probability DP...

And the source of this question is the last time I went to the Mudanjiang semi-finals ..

FML ------ don't want to talk more

DP [I] [J] [k] indicates that K pieces occupy the J column of the I row. <each coordinate is (x, y) the pawns can all occupy column Y in row x>

So if K pieces have been placed and the J column of the I row is occupied, then for the (k + 1) there will be four different cases for each piece. <ensure that there are spaces on the board while they can be placed again>

Here, N * m-K refers to the number of N * m grids that can be placed with K grids.

1.-The k + 1 piece is placed in a column in the J column of the occupied I row.

You can find p1 = (I * j-k)/(n * m-K)

The corresponding state transition equation is DP [I] [J] [k] = DP [I] [J] [k-1] * P1;

2.-This k + 1 piece is placed in one of the occupied J columns but not in one of the occupied I rows.

We can find P2 = (n-I) * j/(n * m-K)

Corresponding to DP [I + 1] [J] [k] = DP [I] [J] [k-1] * P2; // This piece occupies more than one line

3-This k + 1 piece is placed in one of the occupied I rows but not in one of the occupied J columns.

You can obtain P3 = (m-j) * I/(n * m-K)

Corresponding to DP [I] [J + 1] [k] = DP [I] [J] [k-1] * P3; // This piece occupies one more column

4-This k + 1 piece is placed on the unoccupied (n-I) Row and (m-j) column.

Obtain P4 = (n-I) * (m-j)/(n * m-K)

Corresponding to DP [I + 1] [J + 1] [k] = DP [I] [J] [k-1] * P4; // This piece occupies one more row and one column.

In fact, we can find that when n> = 1 & M> = 1, the minimum number of pieces to be placed is 1. the maximum number of pieces to be placed is maxnum = (max (n, m)-1) * min (n, m) + 1

It is better to add sum.

Ans = sigma (DP [N] [m] [k]) k = 1, 2 ,......... Maxnum;

 1 #include <iostream> 2 #include <cstring> 3 #include <iomanip> 4 #include <algorithm> 5 using namespace std; 6  7 const int size = 55; 8 double dp[size][size][size*size]; 9 10 int main()11 {12     int t , n ,m , maxNum;13     double ans;14     cin >> t;15     while( t-- )16     {17         ans = 0;18         memset( dp , 0 , sizeof(dp) );19         dp[0][0][0] = 1;20         cin >> n >> m;21         maxNum = ( max(n,m)-1 ) * min(n,m) + 1;22         for( int i = 1 ; i<=n ; i++ )23         {24             for( int j = 1 ; j<=m ; j++ )25             {26                 for( int k = 1 ; k<=maxNum ; k++ )27                 {28                     dp[i][j][k] = ( dp[i-1][j][k-1] * (n-i+1) * j  + dp[i][j-1][k-1] * (m-j+1) * i  + dp[i-1][j-1][k-1] * (n-i+1) * (m-j+1) ) / ( n*m-k+1 );29                     if( i==n && j==m )30                         continue;31                     else32                         dp[i][j][k] += dp[i][j][k-1] * ( i*j-k+1 ) / (n*m-k+1);33                 }34             }35         }36         for( int k = 1 ; k<=maxNum ; k++ )37         {38             ans += k * dp[n][m][k];39         }40         cout << setiosflags(ios::fixed);41         cout << setprecision(12) << ans <<endl;42     }43     return 0;44 }
View code

 

Today:

Why do we always expect something to come?

Because we artificially increase the probability of occurrence or even approximate 100%.

But what we fear always comes.

Because of our own fear, we are unable to make full use of our ordinary standards.

 

Zoj -- 3822 -- probability DP of the second question

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.