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

 

37995672014-10-14 10:13:59                                                                    Accepted                                                             3822C++187071760njczy2010
37995662014-10-14 10:13:25                                                                    Memory Limit Exceeded                                                             3822C++0131073njczy2010

 

sign,,,太弱了,,,

 

Domination

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

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cstdio>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<map>
 9 #include<set>
10 #include<string>
11 //#include<pair>
12 
13 #define N 55
14 #define M 1000005
15 #define mod 1073741824
16 //#define p 10000007
17 #define mod2 100000000
18 #define ll long long
19 #define LL long long
20 #define maxi(a,b) (a)>(b)? (a) : (b)
21 #define mini(a,b) (a)<(b)? (a) : (b)
22 
23 using namespace std;
24 
25 int T;
26 int n,m;
27 double dp[N][N][N*N];
28 double ans;
29 int tot;
30 
31 void ini()
32 {
33     ans=0;
34     memset(dp,0,sizeof(dp));
35     scanf("%d%d",&n,&m);
36     tot=n*m;
37 }
38 
39 void solve()
40 {
41     int i,j,k;
42     dp[1][1][1]=1;
43     for(i=1;i<=n;i++){
44         for(j=1;j<=m;j++){
45             if(i==1 && j==1) continue;
46             for(k=max(i,j);k<=i*j;k++){
47                 //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)));
48                 if(i==n && j==m){
49                     dp[i][j][k]=dp[i-1][j][k-1]*(n-i+1)*j/(tot-(k-1))
50                             +dp[i][j-1][k-1]*(m-j+1)*i/(tot-(k-1))
51                             +dp[i-1][j-1][k-1]*(n-i+1)*(m-j+1)/(tot-(k-1));
52                 }
53                 else
54                 dp[i][j][k]=dp[i-1][j][k-1]*(n-i+1)*j/(tot-(k-1))
55                             +dp[i][j-1][k-1]*(m-j+1)*i/(tot-(k-1))
56                             +dp[i-1][j-1][k-1]*(n-i+1)*(m-j+1)/(tot-(k-1))
57                             +dp[i][j][k-1]*(i*j-(k-1))/(tot-(k-1));
58             // printf("  aft i=%d j=%d k=%d dp=%.5f\n",i,j,k,dp[i][j][k]);
59             }
60         }
61     }
62     //for(i=1;i<=n;i++){
63     //    for(j=1;j<=m;j++){
64     //        for(k=max(i,j);k<=i*j;k++){
65      //           printf(" i=%d j=%d k=%d dp=%.5f\n",i,j,k,dp[i][j][k]);
66     //        }
67     //    }
68    // }
69 
70     for(k=0;k<=tot;k++){
71         ans+=dp[n][m][k]*k;
72     }
73 }
74 
75 void out()
76 {
77     printf("%.10f\n",ans);
78 }
79 
80 int main()
81 {
82    // freopen("data.in","r",stdin);
83     //freopen("data.out","w",stdout);
84     scanf("%d",&T);
85    // for(int ccnt=1;ccnt<=T;ccnt++)
86     while(T--)
87    // while(scanf("%s",s1)!=EOF)
88     {
89         //if(n==0 && k==0 ) break;
90         //printf("Case %d: ",ccnt);
91         ini();
92         solve();
93         out();
94     }
95 
96     return 0;
97 }

 

转载于:https://www.cnblogs.com/njczy2010/p/4023831.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值