您的位置:首页 > 其它

[zoj 3822]2014牡丹江区域赛 Domination 概率dp求期望

2014-10-12 20:58 357 查看
<span style="font-family: Arial, Helvetica, Verdana, sans-serif; background-color: rgb(255, 255, 255);">Domination</span>


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.666666666667


Author: JIANG, Kai

题目大意

给定n*m的空棋盘

每一次在上面选择一个空的位置放置一枚棋子,直至每一行每一列都至少有一个棋子,求放置次数的期望

解题思路

同步的时候没有做出来

概率应该顺着推,期望应该倒着推。卡在这里了。

然后以为50*50可以预处理所有值直接输出答案,结果越错越远

正解:

dp[i][j][k]表示在m*n的棋盘上已经占据了i行j列,已经选定k个棋子,需要把整个棋盘都占据还需要的次数的期望

答案就是dp[0][0][0]

则每一步的一个新棋子都可以

1)占据新的一行 概率为 ((n-i)*j)/(n*m-k)

2)占据新的一列 概率为 (i*(m-j))/(n*m-k)

3)占据新的一行和新的一列 概率为 ((n-i)*(m-j))/(n*m-k)

4)什么都不做,不占据新的行列 概率为 (i*j-k)/(n*m-k)

dp方程就很好转移啦,见代码

#include <cstdio>
#include <algorithm>

using namespace std;
double dp[51][51][2505];
int main()
{int T,n,m;
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		dp
[m][n*m]=0;
		for (int k=m*n-1;k>=0;k--)
		for (int i=n;i>=0;i--)
			for (int j=m;j>=0;j--)
				if (k>=max(i,j)&&k<=i*j)
				{
					dp[i][j][k]=0;
					if (i==n&&j==m) continue;//当要处理的状态为n*m时直接跳出
					if (i<n)if (j) dp[i][j][k]+=(1+dp[i+1][j][k+1])*(n-i)*j/(n*m-k);
					if (j<m)if (i) dp[i][j][k]+=(1+dp[i][j+1][k+1])*(i)*(m-j)/(n*m-k);
					if (i<n&&j<m) dp[i][j][k]+=(1+dp[i+1][j+1][k+1])*(n-i)*(m-j)/(n*m-k);
					dp[i][j][k]+=(1+dp[i][j][k+1])*(i*j-k)/(n*m-k);
				}
				else dp[i][j][k]=-1;//不可行状态置为-1不影响后续的处理
		printf("%.10f\n",dp[0][0][0]);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: