zoj 3822(概率dp)

ZOJ Problem Set - 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 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 <= N, M <= 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.666666666666

Author: JIANG, Kai
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest

题意:n*m的棋盘中,每下一个棋子会把棋子所在位置的x,y轴覆盖

每天放一个棋子,问所需天数的期望

dp[k][i][j]表上k天覆盖了i条x轴,j条y轴的概率

dp[k][i][j]=dp[k-1][i-1][j-1]*t1+dp[k-1][i][j-1]*t2+dp[k-1][[i][j-1]*t3+dp[k-1][i][j]*t4;

t1 t2 t3 t4对应的概率

特别注意的是只有当i<n||j<m 的时候 dp[k-1][i][j]才有效

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cctype>
 5 #include<cmath>
 6 #include<cstring>
 7 #include<map>
 8 #include<queue>
 9 #include<stack>
10 #include<set>
11 #include<vector>
12 #include<algorithm>
13 #include<string.h>
14 typedef long long ll;
15 typedef unsigned long long LL;
16 using namespace std;
17 const int INF=0x3f3f3f3f;
18 const double eps=0.0000000001;
19 const int N=50+10;
20 const ll mod=1e9+7;
21 double dp[N*N][N][N];
22 int main(){
23     int n,m;
24     int t;
25     scanf("%d",&t);
26     while(t--){
27         scanf("%d%d",&n,&m);
28         memset(dp,0,sizeof(dp));
29         dp[0][0][0]=1;
30         for(int k=1;k<=n*m;k++){
31             for(int i=1;i<=n;i++){
32                 for(int j=1;j<=m;j++){
33                     double t1=0,t2=0,t3=0,t4=0;
34                     double tt=n*m-k+1;
35                     t1=1.0*(n*m-(i-1)*m-(j-1)*(n-i+1))/(tt);
36                     t2=1.0*(n*j-(i-1)*j)/(tt);
37                     t3=1.0*(m*i-(j-1)*i)/(tt);
38                     t4=1.0*(i*j-k+1)/(tt);
39                     dp[k][i][j]+=dp[k-1][i-1][j-1]*t1;
40                     dp[k][i][j]+=dp[k-1][i-1][j]*t2;
41                     dp[k][i][j]+=dp[k-1][i][j-1]*t3;
42                     if(i<n||j<m)
43                     dp[k][i][j]+=dp[k-1][i][j]*t4;
44                 }
45             }
46         }
47         double ans=0;
48         for(int i=1;i<=n*m;i++){
49             ans=ans+dp[i][n][m]*i;
50         }
51         printf("%.11f\n",ans);
52     }
53 }

 

posted on 2018-03-21 20:10  见字如面  阅读(101)  评论(0编辑  收藏  举报

导航