ZOJ 3822 Domination (概率DP)

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

题意:向一个N*M的棋盘里随机放棋子,每天往一个格子里放一个。求每一行每一列

都有棋子覆盖的天数。

思路:如下图所示 ,dp[i][j][k]表示左上角有i行j列被k个棋子覆盖的概率(不管

棋子放到哪,都可以移到左上角的相应地方,k就是区域1中叉的个数)。



当棋子放到区域1时: dp[i][j][k]=(r*c-k)/(n*m-k)*dp[i][j][k+1];

当棋子放到区域2时: dp[i][j][k]=(n-r)*c/(n*m-k)*dp[i+1][j][k+1];

当棋子放到区域3时: dp[i][j][k]=r*(m-c)/(n*m-k)*dp[i][j+1][k+1];

当棋子放到区域4时: dp[i][j][k]=(n-r)*(m-c)/(n*m-k)*dp[i+1][j+1][k+1];

最后注意下放到每个区域对应的条件就OK了。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=55;

double dp[maxn][maxn][maxn*maxn];
bool visited[maxn][maxn][maxn*maxn];
int n,m;

double DP(int r,int c,int num)
{
    if(visited[r][c][num])  return dp[r][c][num];
    if(r==0 || c==0)   return DP(r+1,c+1,num+1)+1;
    if(r>=n && c>=m)   return 0;
    double ans=0.0,p=n*m-num,q=r*c-num;
    if(num<r*c)        ans+=q/p*DP(r,c,num+1);
    if(r<n)            ans+=(n-r)*c/p*DP(r+1,c,num+1);
    if(c<m)            ans+=r*(m-c)/p*DP(r,c+1,num+1);
    if(r<n && c<m)     ans+=(n-r)*(m-c)/p*DP(r+1,c+1,num+1);
    visited[r][c][num]=1;
    return dp[r][c][num]=ans+1;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);
        memset(visited,0,sizeof(visited));
        printf("%.12f\n",DP(0,0,0));
    }
    return 0;
}

时间: 2024-10-26 10:43:55

ZOJ 3822 Domination (概率DP)的相关文章

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

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 chessboar

ZOJ 3822 Domination 概率DP求期望

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 chessboar

ZOJ 3822 Domination 概率DP

题意:在一个n*m的棋盘上放棋子,一个棋子可覆盖一行一列,若n行m列全被覆盖则停止放棋子,求棋子的期望 思路:期望DP, dp[i][j][k]表示放了i个棋子覆盖了j行k列 已知dp[0][0][0]=1,求dp[1~n*m][n][m] 四种情况: 1.再放一个棋子,行列都不增加 dp[i+1][j][k]+=dp[i][j][k]*(j*k-i)*1.0/(m*n-i); 2.只增加一行 dp[i+1][j+1][k]+=dp[i][j][k]*(n-j)*k*1.0/(m*n-i); 3

zoj 3288 Domination (概率dp)

///dp[i][j][k]表示i行j列已经有棋子,且放了k个的概率 ///dp[i][j][k]一共有四种转移方式 ///1:dp[i-1][j][k-1] 概率为 (n-(i-1))*j/(n*m-(k-1)) ///2:dp[i][j-1][k-1] 概率为 i*(m-(j-1))/(n*m-(k-1)) ///3:dp[i-1][j-1][k-1] 概率为 (n-(i-1))*(m-(j-1))/(n*m-(k-1)) ///4:dp[i][j][k-1] 概率为 (i*j-(k-1))

ZOJ - 3822 Domination (DP)

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 piec

[概率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 che

zoj 3822 Domination 【概率DP 求期望】

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 chessboar

14牡丹江现场赛 D ZOJ 3822 Domination

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 chessboar

zoj 3822 Domination(2014牡丹江区域赛D题)

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 chessboar

ZOJ 3822 Domination (三维概率DP)

E - Domination Time Limit:8000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit Status Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he