zoukankan      html  css  js  c++  java
  • Domination

    Domination

    题目链接:https://odzkskevi.qnssl.com/9713ae1d3ff2cc043442f25e9a86814c?v=1531624384

    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 Tindicating the number of test cases. For each test case:

    There are only two integers N and M (1 <= NM <= 50).

    <h4< dd="">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.

    <h4< dd="">Sample Input

    2
    1 3
    2 2
    

    <h4< dd="">Sample Output

    3.000000000000
    2.666666666667

    题意:t组数据 n*m棋盘,让n行m列都存在一个棋子的时候,定义为n*m的棋盘全部被覆盖。最后求期望

    思路:涉及到了概率和期望,当然我们第一时间想到的就是概率DP,开始一直在推算状态转移方程的时候。没有乘上没有出现的次数

    例如dp[i-1][j][k-1] 我们应该要写成dp[i]-1][j][k-1]*p*((n-i+1)*j),也就是此前没出现过的数.(p为当前的剩余天数分之一)

    最后状态转移方程如下

    dp[i][j][k]=dp[i][j-1][k-1]*p*((m-j+1)*i)+dp[i-1][j-1][k-1]*p*((n-i+1)*(m-j+1))+dp[i-1][j][k-1]*p*((n-i+1)*j)+dp[i][j][k-1]*(i*j-(k-1))*p

    当i==n&&j==m时我们不需要加上dp[i][j][k-1]*(i*j-(k-1))*p;或者我们在求期望的时候 累加 (dp[n][m][i]-dp[n][m][i-1])*i;

    最后我们累加dp[n][m[i]*i即可 求出期望;

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<map>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<set>
    #include<cmath>
    using namespace std;
    typedef long long ll;
    typedef pair<ll, int> pll;
    #define eps 1e-6
    const int INF = 0x3f3f3f3f;
    const int maxn = 1000000+5;
    const int MOD = 1e9+7;
    double dp[55][55][2505];
    int main()
    {
        int t,n,m;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d %d",&n,&m);
            memset(dp,0,sizeof(dp));
            dp[0][0][0]=1;
            for(int i=1; i<=n; i++)
                for(int j=1; j<=m; j++)
                    for(int k=1; k<=n*m; k++)
                    {
                        double p=1.0/(n*m-k+1);
                        if(i==n&&j==m) 
                        {
                            dp[i][j][k]=dp[i][j-1][k-1]*p*((m-j+1)*i)+dp[i-1][j-1][k-1]*p*((n-i+1)*(m-j+1))+dp[i-1][j][k-1]*p*((n-i+1)*j);
                        }
    
                        else
                            dp[i][j][k]=dp[i][j-1][k-1]*p*((m-j+1)*i)+dp[i-1][j-1][k-1]*p*((n-i+1)*(m-j+1))+dp[i-1][j][k-1]*p*((n-i+1)*j)+dp[i][j][k-1]*(i*j-(k-1))*p; 
                    }
            double sum=0;
            for(int i=1; i<=n*m; i++)
            {
                sum+=dp[n][m][i]*i;
            }
            printf("%.12lf
    ",sum);
        }
    }
    View Code

    PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

  • 相关阅读:
    黄聪:解决Web部署 svg/woff/woff2字体 404错误
    黄聪:C#中HtmlAgilityPack判断是否包含或不包含指定的属性或值
    黄聪:GeckoFX如何引用jquery文件并执行自定义JS
    黄聪:css3实现图片划过一束光闪过效果(图片光影掠过效果)
    黄聪:C#带cookie模拟登录百度
    黄聪:如何为IIS增加svg和woff等字体格式的MIME
    黄聪:微信支付错误两个问题的解决:curl出错,错误码:60
    黄聪:《跟黄聪学WordPress插件开发》
    黄聪:GeckoWebBrowser多窗口独立cookie
    黄聪:远程连接mysql数据库注意事项记录(远程连接慢skip-name-resolve)
  • 原文地址:https://www.cnblogs.com/MengX/p/9314224.html
Copyright © 2011-2022 走看看