zoukankan      html  css  js  c++  java
  • ZOJ 3822 Domination 期望dp

    Domination

    Time Limit: 1 Sec  

    Memory Limit: 256 MB

    题目连接

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3822

    Description

    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 dominatedby 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 <= NM <= 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

    HINT

    题意

    每次这个人会随机选择一个空格子扔棋子,然后问你期望扔多少次,可以把n*m的矩阵,每一行和每一列都至少有一个棋子

    题解:

    期望dp,用容斥做

    dp[i][j][k]表示占领了i行j列,用了k个

    代码:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <stack>
    #include <map>
    #include <set>
    #include <queue>
    #include <iomanip>
    #include <string>
    #include <ctime>
    #include <list>
    #include <bitset>
    typedef unsigned char byte;
    #define pb push_back
    #define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
    #define local freopen("in.txt","r",stdin)
    #define pi acos(-1)
    
    using namespace std;
    const int maxn = 50 + 10;
    double dp[maxn][maxn][maxn*maxn];
    int n , m ;
    
    
    inline double GetDouble(int x)
    {
        return (double)x;
    }
    
    
    void initiation()
    {
        memset(dp,-1,sizeof(dp));
        scanf("%d%d",&n,&m);
    }
    
    double dfs(int x,int y,int k)
    {
        if(dp[x][y][k]> -0.5) return dp[x][y][k];
        double & ans = dp[x][y][k] = 0;
        if(x == n && y == m ) return ans;
        int all = m*n-k;
        if(x*y != k) ans += dfs(x,y,k+1)*GetDouble(x*y-k)/GetDouble(all);
        if(x != n && y != m) ans += dfs(x+1,y+1,k+1)*GetDouble((n-x)*(m-y))/GetDouble(all);
        if(x != n && y != 0) ans += dfs(x+1,y,k+1)*GetDouble(y*(n-x))/GetDouble(all);
        if(y != m && x != 0) ans += dfs(x,y+1,k+1)*GetDouble(x*(m-y))/GetDouble(all);
        ans += 1;
        return ans;
    }
    
    double solve()
    {
        return dfs(0,0,0);
    }
    
    int main(int argc,char *argv[])
    {
        int Case;
        scanf("%d",&Case);
        while(Case--)
        {
            initiation();
            printf("%.12lf
    ",solve());
        }
        return 0;
    }
  • 相关阅读:
    MyISAM表锁的解决方案
    RSA数字证书管理
    Self Host WebApi服务传输层SSL加密(服务器端+客户端调用)
    WebApi服务Uri加密及验证的两种方式
    利用MVC的自定义过滤器FilterAttribute、IActionFilter、IExceptionFilter实现异常处理等功能
    html页面中meta的作用
    [转]REST简介
    [转]webApi 参数传递总结
    REST服务中的异常处理
    REST服务返回自定义的HttpResponseMessage
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4836490.html
Copyright © 2011-2022 走看看