您的位置:首页 > 其它

zoj 3822 Domination(2014牡丹江区域赛D题) (概率dp)

2014-10-14 10:23 411 查看
37995672014-10-14 10:13:59Accepted 3822C++187071760njczy2010
37995662014-10-14 10:13:25Memory Limit Exceeded 3822C++0131073njczy2010
sign,,,太弱了,,,

DominationTime 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

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<string>
//#include<pair>

#define N 55
#define M 1000005
#define mod 1073741824
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b)

using namespace std;

int T;
int n,m;
double dp

[N*N];
double ans;
int tot;

void ini()
{
ans=0;
memset(dp,0,sizeof(dp));
scanf("%d%d",&n,&m);
tot=n*m;
}

void solve()
{
int i,j,k;
dp[1][1][1]=1;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(i==1 && j==1) continue;
for(k=max(i,j);k<=i*j;k++){
//printf("  i=%d j=%d k=%d dp=%.5f add=%.5f\n",i,j,k,dp[i][j-1][k-1],dp[i][j-1][k-1]*(m-j)*i/(tot-(k-1)));
if(i==n && j==m){
dp[i][j][k]=dp[i-1][j][k-1]*(n-i+1)*j/(tot-(k-1))
+dp[i][j-1][k-1]*(m-j+1)*i/(tot-(k-1))
+dp[i-1][j-1][k-1]*(n-i+1)*(m-j+1)/(tot-(k-1));
}
else
dp[i][j][k]=dp[i-1][j][k-1]*(n-i+1)*j/(tot-(k-1))
+dp[i][j-1][k-1]*(m-j+1)*i/(tot-(k-1))
+dp[i-1][j-1][k-1]*(n-i+1)*(m-j+1)/(tot-(k-1))
+dp[i][j][k-1]*(i*j-(k-1))/(tot-(k-1));
// printf("  aft i=%d j=%d k=%d dp=%.5f\n",i,j,k,dp[i][j][k]);
}
}
}
//for(i=1;i<=n;i++){
//    for(j=1;j<=m;j++){
//        for(k=max(i,j);k<=i*j;k++){
//           printf(" i=%d j=%d k=%d dp=%.5f\n",i,j,k,dp[i][j][k]);
//        }
//    }
// }

for(k=0;k<=tot;k++){
ans+=dp
[m][k]*k;
}
}

void out()
{
printf("%.10f\n",ans);
}

int main()
{
// freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d",&T);
// for(int ccnt=1;ccnt<=T;ccnt++)
while(T--)
// while(scanf("%s",s1)!=EOF)
{
//if(n==0 && k==0 ) break;
//printf("Case %d: ",ccnt);
ini();
solve();
out();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: