当前位置: 首页 > 工具软件 > Domination > 使用案例 >

ZOJ 3822 Domination 期望dp

呼延智明
2023-12-01

Domination

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

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

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 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 dominatedby 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

HINT

 

题意

每次这个人会随机选择一个空格子扔棋子,然后问你期望扔多少次,可以把n*m的矩阵,每一行和每一列都至少有一个棋子

题解:

期望dp,用容斥做

dp[i][j][k]表示占领了i行j列,用了k个

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1)

using namespace std;
const int maxn = 50 + 10;
double dp[maxn][maxn][maxn*maxn];
int n , m ;


inline double GetDouble(int x)
{
    return (double)x;
}


void initiation()
{
    memset(dp,-1,sizeof(dp));
    scanf("%d%d",&n,&m);
}

double dfs(int x,int y,int k)
{
    if(dp[x][y][k]> -0.5) return dp[x][y][k];
    double & ans = dp[x][y][k] = 0;
    if(x == n && y == m ) return ans;
    int all = m*n-k;
    if(x*y != k) ans += dfs(x,y,k+1)*GetDouble(x*y-k)/GetDouble(all);
    if(x != n && y != m) ans += dfs(x+1,y+1,k+1)*GetDouble((n-x)*(m-y))/GetDouble(all);
    if(x != n && y != 0) ans += dfs(x+1,y,k+1)*GetDouble(y*(n-x))/GetDouble(all);
    if(y != m && x != 0) ans += dfs(x,y+1,k+1)*GetDouble(x*(m-y))/GetDouble(all);
    ans += 1;
    return ans;
}

double solve()
{
    return dfs(0,0,0);
}

int main(int argc,char *argv[])
{
    int Case;
    scanf("%d",&Case);
    while(Case--)
    {
        initiation();
        printf("%.12lf\n",solve());
    }
    return 0;
}

 

 类似资料: