Domination

Posted mengx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

以上是关于Domination的主要内容,如果未能解决你的问题,请参考以下文章

[概率dp] zoj 3822 Domination

ZOJ Problem Set - 3822Domination(DP)

ZOJ 3822 Domination(概率dp)

ZOJ 3822 Domination 概率DP

zoj 3822 Domination 概率dp 2014牡丹江站D题

微信小程序代码片段