[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; }
相关文章推荐
- ZOJ 3822 Domination The 2014 ACM-ICPC 牡丹江区域赛 概率dp 先算概率,再转成期望
- zoj 3822 Domination(2014牡丹江区域赛D题) (概率dp)
- ZOJ-3822-Domination【概率dp】【2014牡丹江赛区】
- ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)
- ZOJ 3822 Domination 概率DP 2014年ACM_ICPC亚洲区域赛牡丹江现场赛D题
- zoj 3822 Domination 概率dp 2014牡丹江站D题
- ZOJ 3822 Domination 概率DP求期望
- zoj 3822 (2014 牡丹江区域赛 D) Domination
- zoj 3822 Domination 【概率DP 求期望】
- ZOJ3822 ACM-ICPC 2014 亚洲区域赛牡丹江赛区现场赛D题Domination 概率DP(两种解法)
- ZOJ 3822 Domination(概率dp 牡丹江现场赛)
- ZOJ 3822 Domination(概率dp 牡丹江现场赛)
- zoj 3822 Domination 概率dp 2014牡丹江站D题
- zoj 3822 Domination(2014牡丹江区域赛D称号)
- ZOJ3822 ACM-ICPC 2014 亚洲区域赛牡丹江赛区现场赛D题Domination 概率DP
- zoj 3822 Domination(2014牡丹江区域赛D题)
- 2014牡丹江现场赛A题D题I题(水,概率Dp,水)ZOJ 3819,3822,3827
- zoj 3822 Domination (概率dp 天数期望)
- ZOJ 3822 Domination (三维概率DP)
- 2014牡丹江区域赛D(概率DP)ZOJ3822