zoukankan      html  css  js  c++  java
  • [概率dp] zoj 3822 Domination

    题目链接:

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?

    problemCode=3822

    Domination

    Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge

    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 chessboard with N rows and M columns.

    Every day after work, Edward will place a chess piece on a random empty cell. A few days later, he found the chessboard was dominated by 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 chessboard of N × M dominated. Please write a program to help him.

    Input

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

    There are only two integers N and M (1 <= NM <= 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

    2
    1 3
    2 2
    

    Sample Output

    3.000000000000
    2.666666666667
    

    Author: JIANG, Kai
    Submit    Status
    题目意思:

    给一个n*m的矩阵,每天放一个球到一个格子里。求要达到每一行和每一列都至少有一个球,所需的天数的期望。

    解题思路:

    概率dp

    dp[i][j][k]:表示已经占用了i行,j列。放了k个球。达到终于要求所需的天数的期望。

    显然:

    1、当不添加行和列时,dp[i][j][k]=(i*j-k)/(n*m-k)*(1+dp[i][j][k+1])

    2、当添加行和列时。dp[i][j][k]=((n-i)*(m-j))/(n*m-k)*(1+dp[i+1][j+1][k+1])

    3、当仅仅添加行时,dp[i][j][k]=(n-i)*j/(n*m-k)*(1+dp[i+1][j][k+1])

    4、当仅仅添加列时。dp[i][j][k]=(m-j)*i/(n*m-k)*(1+dp[i][j+1][k+1])

    代码:

    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    
    #define Maxn 55
    
    double dp[Maxn][Maxn][Maxn*Maxn];
    int n,m;
    
    int main()
    {
        int t;
    
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            for(int i=0;i<=n+1;i++)
                for(int j=0;j<=m+1;j++)
                    for(int k=0;k<=(n+1)*(m+1);k++)
                        dp[i][j][k]=-1;
            int Ma=max(n*m-n+1,n*m-m+1);
            int Mi=max(n,m);
    
            for(int i=Mi;i<=Ma;i++)
                dp[n][m][i]=0;
    
            for(int i=n;i>=0;i--)
            {
                for(int j=m;j>=0;j--)
                {
                    if(i==n&&j==m)
                        continue;
                    Ma=i*j;
                    Mi=max(i,j);
                    for(int k=Ma;k>=Mi;k--)
                    {
                        if(dp[i][j][k+1]!=-1)
                        {
                            if((n*m-k))
                            {
                                if(dp[i][j][k]!=-1)
                                    dp[i][j][k]+=(Ma-k*1.0)/(n*m-k*1.0)*(1+dp[i][j][k+1]);
                                else
                                    dp[i][j][k]=(Ma-k*1.0)/(n*m-k*1.0)*(1+dp[i][j][k+1]);
                            }
                        }
                        if(dp[i+1][j+1][k+1]!=-1)
                        {
                            if(n*m-k)
                            {
                                if(dp[i][j][k]!=-1)
                                    dp[i][j][k]+=((n-i)*(m-j)*1.0)/(n*m-k*1.0)*(1+dp[i+1][j+1][k+1]);
                                else
                                    dp[i][j][k]=((n-i)*(m-j)*1.0)/(n*m-k*1.0)*(1+dp[i+1][j+1][k+1]);
                            }
    
                        }
                        if(dp[i][j+1][k+1]!=-1)
                        {
                            if(n*m-k)
                            {
                                if(dp[i][j][k]!=-1)
                                    dp[i][j][k]+=((m-j)*i*1.0)/(n*m-k*1.0)*(1+dp[i][j+1][k+1]);
                                else
                                    dp[i][j][k]=((m-j)*i*1.0)/(n*m-k*1.0)*(1+dp[i][j+1][k+1]);
                            }
    
                        }
                        if(dp[i+1][j][k+1]!=-1)
                        {
                            if(n*m-k)
                            {
                                if(dp[i][j][k]!=-1)
                                    dp[i][j][k]+=((n-i)*j*1.0)/(n*m-k*1.0)*(1+dp[i+1][j][k+1]);
                                else
                                    dp[i][j][k]=((n-i)*j*1.0)/(n*m-k*1.0)*(1+dp[i+1][j][k+1]);
                            }
                        }
                    }
    
                }
            }
            /*for(int i=n;i>=0;i--)
                for(int j=m;j>=0;j--)
                    for(int k=i*j;k>=0;k--)
                        printf("i:%d j:%d k:%d %lf
    ",i,j,k,dp[i][j][k]);*/
            printf("%.10lf
    ",dp[0][0][0]);
        }
        return 0;
    }


  • 相关阅读:
    java——base64 加密和解密
    BASE64Encoded() 方法报错说方法未定义
    java 实现HTTP连接(HTTPClient)
    如何关闭一些烦人的弹窗(总结)
    IDEA
    6、git和github
    5、预测和鉴定miRNA的靶基因
    4、在线blast比对结果解析(保守结构域)
    ASE分析
    3、Linux下配置Java环境
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7221949.html
Copyright © 2011-2022 走看看