ZOJ 3822 Domination 期望dp

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个

ZOJ 3822 Domination 期望dp

代码:

#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 = + ;
double dp[maxn][maxn][maxn*maxn];
int n , m ; inline double GetDouble(int x)
{
return (double)x;
} void initiation()
{
memset(dp,-,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] = ;
if(x == n && y == m ) return ans;
int all = m*n-k;
if(x*y != k) ans += dfs(x,y,k+)*GetDouble(x*y-k)/GetDouble(all);
if(x != n && y != m) ans += dfs(x+,y+,k+)*GetDouble((n-x)*(m-y))/GetDouble(all);
if(x != n && y != ) ans += dfs(x+,y,k+)*GetDouble(y*(n-x))/GetDouble(all);
if(y != m && x != ) ans += dfs(x,y+,k+)*GetDouble(x*(m-y))/GetDouble(all);
ans += ;
return ans;
} double solve()
{
return dfs(,,);
} int main(int argc,char *argv[])
{
int Case;
scanf("%d",&Case);
while(Case--)
{
initiation();
printf("%.12lf\n",solve());
}
return ;
}
上一篇:NOIP2008提高组(前三题) -SilverN


下一篇:DDD(领域驱动设计)思想解读及优秀实践MK