标签:style http color io os ar for strong sp
题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3822
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 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).
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.
2 1 3 2 2
3.000000000000 2.666666666667
给一个n*m的矩阵,每天放一个球到一个格子里,求要达到每一行和每一列都至少有一个球,所需的天数的期望。
解题思路:
概率dp
dp[i][j][k]:表示已经占用了i行,j列,放了k个球,达到最终要求所需的天数的期望。
显然:
1、当不增加行和列时,dp[i][j][k]=(i*j-k)/(n*m-k)*(1+dp[i][j][k+1])
2、当增加行和列时,dp[i][j][k]=((n-i)*(m-j))/(n*m-k)*(1+dp[i+1][j+1][k+1])
3、当只增加行时,dp[i][j][k]=(n-i)*j/(n*m-k)*(1+dp[i+1][j][k+1])
4、当只增加列时,dp[i][j][k]=(m-j)*i/(n*m-k)*(1+dp[i][j+1][k+1])
代码:
#include<cstdio> #include<cstdlib> #include<algorithm> using namespace std; #define Maxn 55 double dp[Maxn][Maxn][Maxn*Maxn]; int n,m; int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0;i<=n+1;i++) for(int j=0;j<=m+1;j++) for(int k=0;k<=(n+1)*(m+1);k++) dp[i][j][k]=-1; int Ma=max(n*m-n+1,n*m-m+1); int Mi=max(n,m); for(int i=Mi;i<=Ma;i++) dp[n][m][i]=0; for(int i=n;i>=0;i--) { for(int j=m;j>=0;j--) { if(i==n&&j==m) continue; Ma=i*j; Mi=max(i,j); for(int k=Ma;k>=Mi;k--) { if(dp[i][j][k+1]!=-1) { if((n*m-k)) { if(dp[i][j][k]!=-1) dp[i][j][k]+=(Ma-k*1.0)/(n*m-k*1.0)*(1+dp[i][j][k+1]); else dp[i][j][k]=(Ma-k*1.0)/(n*m-k*1.0)*(1+dp[i][j][k+1]); } } if(dp[i+1][j+1][k+1]!=-1) { if(n*m-k) { if(dp[i][j][k]!=-1) dp[i][j][k]+=((n-i)*(m-j)*1.0)/(n*m-k*1.0)*(1+dp[i+1][j+1][k+1]); else dp[i][j][k]=((n-i)*(m-j)*1.0)/(n*m-k*1.0)*(1+dp[i+1][j+1][k+1]); } } if(dp[i][j+1][k+1]!=-1) { if(n*m-k) { if(dp[i][j][k]!=-1) dp[i][j][k]+=((m-j)*i*1.0)/(n*m-k*1.0)*(1+dp[i][j+1][k+1]); else dp[i][j][k]=((m-j)*i*1.0)/(n*m-k*1.0)*(1+dp[i][j+1][k+1]); } } if(dp[i+1][j][k+1]!=-1) { if(n*m-k) { if(dp[i][j][k]!=-1) dp[i][j][k]+=((n-i)*j*1.0)/(n*m-k*1.0)*(1+dp[i+1][j][k+1]); else dp[i][j][k]=((n-i)*j*1.0)/(n*m-k*1.0)*(1+dp[i+1][j][k+1]); } } } } } /*for(int i=n;i>=0;i--) for(int j=m;j>=0;j--) for(int k=i*j;k>=0;k--) printf("i:%d j:%d k:%d %lf\n",i,j,k,dp[i][j][k]);*/ printf("%.10lf\n",dp[0][0][0]); } return 0; }
标签:style http color io os ar for strong sp
原文地址:http://blog.csdn.net/cc_again/article/details/40020689