ZOJ 3822 Domination(DP)
2014-10-15 22:41
267 查看
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.
There are only two integers N and M (1 <= N, M <= 50).
Any solution with a relative or absolute error of at most 10-8 will be accepted.
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
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<limits.h> typedef long long LL; using namespace std; double dp[55][55][55*55]; int t,n,m; int main() { scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(dp,0,sizeof(dp)); dp[1][1][1]=1.0; for(int k=1;k<n*m;k++) { for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(dp[k][i][j]>0) { dp[k+1][i+1][j+1]+=dp[k][i][j]*(n-i)*(m-j)/(n*m-k); dp[k+1][i+1][j]+=dp[k][i][j]*(n-i)*j/(n*m-k); dp[k+1][i][j+1]+=dp[k][i][j]*(m-j)*i/(n*m-k); if(i<n||j<m&&i*j>k) dp[k+1][i][j]+=dp[k][i][j]*(i*j-k)/(n*m-k); } } } } double ans=0; for(int i=1;i<=n*m;i++) ans+=dp[i] [m]*i; printf("%.8f\n",ans); } return 0; }
相关文章推荐
- ZOJ 3822 Domination 概率DP入门
- ZOJ - 3822 Domination (DP)
- ZOJ 3822 Domination (概率DP)
- ZOJ - 3822 Domination (DP)
- [概率dp] ZOJ 3822 Domination
- zoj 3822 Domination(dp)
- zoj 3822 Domination(dp)
- ZOJ 3822 Domination 期望dp
- ZOJ Problem Set - 3822Domination(DP)
- ZOJ3822——概率DP——Domination
- ZOJ 3822 Domination【概率dp】
- ZOJ 3822 Domination(DP,亚洲区域赛牡丹江站)
- ZOJ 3822 Domination(概率dp)
- [zoj 3822]2014牡丹江区域赛 Domination 概率dp求期望
- zoj 3822 Domination (概率dp 天数期望)
- ZOJ 3822 Domination [概率DP]
- ZOJ 3822 Domination(概率DP)
- zoj 3822 Domination(2014牡丹江区域赛D题) (概率dp)
- ZOJ 3822 Domination(概率DP)
- zoj 3822 Domination 概率dp 2014牡丹江站D题