ACM learning process -- zoj 3822 domination (question D of 2014 Mudanjiang semi-finals) (probability, mathematical recursion)

Source: Internet
Author: User

Description

Edward is the headmaster of marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboardNRows andMColumns.

Every day after work, Edward will place a chess piece on a random empty cell. A few days later, he found the chessboard wasDominatedBy the chess pieces. That means there is 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 chessboardN×MDominated. Please write a program to help him.

Input

There are multiple test cases. The first line of input contains an integerTIndicating the number of test cases. For each test case:

There are only two integersNAndM(1 <=N,M<= 50 ).

Output

For each test case, output the expectation number of days.

Any solution with a relative or absolute error of at most 10-8 will be accepted.

Sample Input

21 32 2

Sample output

3.20.000000002.666666666667


In this question, we can set a probability function P (I, j, k) to represent the probability to overwrite column J in the I row with K stones. We can first calculate all P values, then, the probability multiplication day sum is used to obtain the expectation.
However, for the recurrence of probability, you can consider the following four situations:
1. By P (I-1, J-1, k-1) in the row not covered cut its column not covered in the grid put a stone;
2, by P (I-1, J, k-1) put a stone in a row not covered;
3. Put a stone in a column not covered by P (I, J-1, k-1;
4, by P (I-1, J-1, k-1) in the covered rows and put a column in the grid stone;
However, note that when calculating P (n, m ,?) You do not need to add the fourth item.
However, each type has its own probability coefficient, which is embodied in the Code. (Number of grids meeting the condition/total number of remaining grids) PS: The key is to find the number of grids meeting the condition.


Code:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <set>#include <map>#include <vector>#include <string>#include <queue>#define inf 0x3fffffff#define esp 1e-10#define N 55using namespace std;double p[N][N][N*N];double qt (int n, int m){    memset (p, 0, sizeof(p));    int len = n*m;    p[0][0][0] = 1;    for (int i = 1; i <= n; ++i)    {        for (int j = 1; j <= m; ++j)        {            for (int k = 1; k <= len; ++k)            {                p[i][j][k] = p[i-1][j-1][k-1]/(n*m-k+1)*(n-i+1)*(m-j+1);                p[i][j][k] += p[i-1][j][k-1]/(n*m-k+1)*(n-i+1)*j;                p[i][j][k] += p[i][j-1][k-1]/(n*m-k+1)*i*(m-j+1);                if (i != n || j != m)                    p[i][j][k] += p[i][j][k-1]/(n*m-k+1)*(i*j-k+1);            }        }    }    double ans = 0;    for (int k = 0; k <= len; ++k)        ans += p[n][m][k]*k;    return ans;}int main(){    //freopen ("test.txt", "r", stdin);    int T;    scanf ("%d", &T);    for (int times = 0; times < T; ++times)    {        int n, m;        scanf ("%d%d", &n, &m);        printf ("%.10lf\n", qt (n, m));    }    return 0;}

 



ACM learning process -- zoj 3822 domination (question D of 2014 Mudanjiang semi-finals) (probability, mathematical recursion)

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.