admin管理员组文章数量:1582665
题目链接:Domination
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 <= 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.666666666667
Author: JIANG, Kai
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest
大致题意:有一个人下棋,每天任意在n行m列的棋盘上随便放一个棋子,问使得棋盘每行且每列都至少有一个元素的天数的期望。
解题思路:可用dp递推出各种情况的概率,然后再根据期望公式:E = pi * ki (其中i = 1, 2, 3……)。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
double dp[60][60][2510];
int main(){
// freopen("in.txt","r",stdin);
int T, m, n;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
for(int i=1; i<=n; i++){ //row
for(int j=1; j<=m; j++){ //column
for(int k=1; k<=m*n; k++){ //day
double tep = m * n - (k - 1);
if(i == n && j == m){
dp[i][j][k] = dp[i-1][j][k-1] * ((n - i + 1) * j) / tep +
dp[i][j-1][k-1] * ((m - j + 1) * i) / tep +
dp[i-1][j-1][k-1] * (n - i + 1) * (m - j + 1) / tep;
}
else{
dp[i][j][k] = dp[i-1][j][k-1] * ((n - i + 1) * j) / tep +
dp[i][j-1][k-1] * ((m - j + 1) * i) / tep +
dp[i-1][j-1][k-1] * (n - i + 1) * (m - j + 1) / tep +
dp[i][j][k-1] * (i * j - (k - 1)) / tep ;
}
}
}
}
double ans = 0;
for(int i=1; i<=m*n; i++)
ans += dp[n][m][i] * i;
printf("%.12lf\n", ans);
}
return 0;
}
版权声明:本文标题:2014ACMICPC亚洲区域赛牡丹江站现场赛-D ( ZOJ 3822 ) Domonation 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1727895099a1136707.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论