首页 > 代码库 > ZOJ 3288 Domination
ZOJ 3288 Domination
Time Limit:8000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu
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 <= 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
21 32 2
Sample Output
3.0000000000002.666666666667
解题:比较简单的一道题,可惜学渣的dp能力太弱了,dp[i][j][k]表示已经覆盖了i行j列且使用了k颗棋子时的概率。。。。
注意转移,放一颗棋子后可能行和列的覆盖数不会增加,也有可能只增加了行,也有可能只增加了列,还有可能同时增加了行列的覆盖数。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 55;18 double dp[maxn][maxn][maxn*maxn];19 int main() {20 int t,n,m;21 scanf("%d",&t);22 while(t--){23 scanf("%d %d",&n,&m);24 memset(dp,0,sizeof(dp));25 dp[0][0][0] = 1;26 for(int i = 1; i <= n; ++i){27 for(int j = 1; j <= m; ++j){28 for(int k = max(i,j);k <= i*j; ++k){29 double p1 = dp[i][j][k-1]*(i*j - k + 1);//行列覆盖都不增加30 double p2 = dp[i-1][j][k-1]*(n-i+1)*j;//增加行31 double p3 = dp[i][j-1][k-1]*i*(m - j + 1);//增加列32 double p4 = dp[i-1][j-1][k-1]*(n - i + 1)*(m - j + 1);//既增加行 又增加列33 dp[i][j][k] = (p1 + p2 + p3 + p4)/(n*m - k + 1);34 }35 }36 }37 double ans = 0;38 for(int i = max(n,m); i <= n*m; ++i)39 ans += dp[n][m][i]*i - dp[n][m][i-1]*i;40 printf("%.9f\n",ans);41 }42 return 0;43 }
ZOJ 3288 Domination