zoukankan      html  css  js  c++  java
  • zoj 3822 Domination(2014牡丹江区域赛D题) (概率dp)

    3799567 2014-10-14 10:13:59                                                                     Accepted                                                             3822 C++ 1870 71760 njczy2010
    3799566 2014-10-14 10:13:25                                                                     Memory Limit Exceeded                                                             3822 C++ 0 131073 njczy2010

    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
    ",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
    ",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
    ",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
    ",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 }
  • 相关阅读:
    第5章:文本处理
    第4章:查找与替换
    第3章:正则表达式
    命令行获取苹果电脑的主要硬件配置
    Linux的tree命令
    log4j.xml的实用例子
    史上最牛js
    Some warning were found during validation
    记十多年前的一次培训经历
    Mac OS X上IntelliJ IDEA 13与Tomcat 8的Java Web开发环境搭建
  • 原文地址:https://www.cnblogs.com/njczy2010/p/4023831.html
Copyright © 2011-2022 走看看