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

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
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest

这条题目是14年牡丹江现场赛的一条期望DP。

给出了一个 N*M 的矩阵 , 问到达每一行且每一列都存在棋子的状态的期望 。

训练的时候这条题是我和小邪一起想的 。

一开始我很快就想到了 设一个状态 dp[i][j][k] 表示已经有i行有棋子 , j列有棋子 , 已经用了k只棋子。

然后其意义是距离目标 (每一行且每一列都存在棋子)的期望。

那么要想出一个转移方程 。

那么这个状态能够转移到的状态就无非4个

dp[i+1][j+1][k+1] , dp[i+1][j][k+1] , dp[i][j+1][k+1],dp[i][j][k+1] .

然后边界和公式都是小邪推的。期望DP写得少的我一开始还是傻乎乎的 。

其实真的不难,就是一些边界要注意 。

那么对于

i*m + j*n - i*j 表示已经覆盖行列的面积 , 那么除了这部分面积以外的部分 ( 即  n*m - ( i*m + j*n - i*j )  ),

可以通过加一只棋推到状态 .   dp[i][j][k] =( n*m - ( i*m + j*n - i*j )  / ( n*m - k)  ) * dp[i+1][j+1][k+1]  ;

在已经被覆盖的面积处在某些地方放一只棋子只能使得行数加 1 , 即在列已被覆盖的点上找行未被覆盖的点的数量 ,

画出图后发现 , 对每一列的点 , 除了与已覆盖的行的交点之外都符合 , 即 ( j * n - i * j )

那么  dp[i][j][k] = ( j * n - i *j )  /( n*m - k)   * dp[i+1][j][k+1] ;

那么上述的行只能加1也能推广到列只能加 1 ,即

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

最后就是一些 行列都不能加1的点 有多少个呐 。。  无疑就是 i*j - k 个了 。。

因为加入一只棋 , 必然占领一行一列 , 那么它必然在已被占领的行列的交点当中 。 即

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

那么每一个条件能进入的边界都是 分子大于 0 分母大于 0 吧 ~  并且  i < n ,j < m

最后 dp[i][j][k] ++ ;

当 i >= m && j >=n 的时候达到目标 返回 0

#include <bits/stdc++.h>
using namespace std;
const int N = 55 ;
bool vis[N][N][N*N];
double dp[N][N][N*N];
int n , m ;
double DP ( int i , int j , int k )
{
    if( i >= n && j >= m ) return 0.0 ;

    if( !vis[i][j][k] ){
        vis[i][j][k] = true ;
        double ans = 0.0 ;
        if( i < n && j < m && ( n*m - ( i*m + j*n - i*j ) ) > 0 ){
            ans +=DP( i + 1 , j + 1 , k + 1 )*( n*m - ( i*m + j*n - i*j ) ) / ( n*m - k) ;
        }
        if( i < n &&( j*n - i*j )  > 0  ){
            ans += DP( i + 1 ,j , k + 1 )*( j*n - i*j )/( n*m - k) ;
        }
        if( j < m &&( i*m - i*j ) > 0  ){
            ans += DP( i ,j + 1 , k + 1 )*( i*m - i*j )/( n*m - k );
        }
        if( i * j - k  > 0 ){
            ans += DP( i , j , k + 1 ) *  ( i * j - k ) /( n*m - k );
        }
        ans += 1.0 ;
        dp[i][j][k] = ans ;
    }
    return dp[i][j][k];
}
int main()
{
    int _;
    cin >> _ ;
    while( _-- ){
        memset( vis , false , sizeof vis ) ;
        cin >> n >> m ;
        printf("%.10lf\n",DP(0,0,0));
    }
}
时间: 2024-10-15 00:44:23

ZOJ 3822 Domination的相关文章

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

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

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 【概率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 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(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