• ZOJ Problem Set


    www.cnblogs.com/shaokele/


    Domination##

      Time Limit: 8 Seconds
      Memory Limit: 131072 KB Special Judge

    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 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
      Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest

    题目地址 ZOJ Problem Set - 3822
    题目大意:一个n*m的棋盘,每次随机在上面落子,问当每行每列都有棋子时步数的期望www.cnblogs.com/shaokele/


    题解####

    简单的概率dp
    状态:(dp[k][i][j])表示当前 (i)(j) 列都符合条件时下了 (k)
    转移:(dp[k)+(1][i][j])+=(dp[k][i][j]*(i*j-k)/(N*M-k);(k<i*j))
       表示新的棋子行号列号都选过了
       (dp[k)+(1][i)+(1][j])+=(dp[k][i][j]*(N-i)*j/(N*M-k);(i+1<=N))
       表示新的棋子行号没选过,列号选过了
       (dp[k)+(1][i][j)+(1])+=(dp[k][i][j]*i*(M-j)/(N*M-k);(j+1<=M))
       表示新的棋子列号没选过,行号选过了
       (dp[k)+(1][i)+(1][j)+(1])+=(dp[k][i][j])((N-i))((M-j)/(N)(M-k);(i)+(1)<=(N,j)+(1)<=(M))
       
    表示新的棋子行号没写过,列号也没选过*
    答案就是(sum_{i=1}^{N*M}i*dp[i][N][M])


    AC代码

    #include <cstdio> 
    #include <cstring>
    using namespace std;
    const int N=55;
    int Q,n,m;
    double dp[N*N][N][N];
    int main(){
    	scanf("%d",&Q);
    	while(Q--){
    		scanf("%d%d",&n,&m);
    		memset(dp,0,sizeof(dp));
    		dp[0][0][0]=1.0;
    		for(int k=0;k<=n*m;k++)
    			for(int i=0;i<=n;i++)
    				for(int j=0;j<=m;j++){
    					if(i==n && j==m ||dp[k][i][j]==0)continue;
    					if(k<i*j)dp[k+1][i][j]+=dp[k][i][j]*(i*j-k)/(n*m-k);
    					if(i+1<=n)dp[k+1][i+1][j]+=dp[k][i][j]*(n-i)*j/(n*m-k);
    					if(j+1<=m)dp[k+1][i][j+1]+=dp[k][i][j]*i*(m-j)/(n*m-k);
    					if(i+1<=n && j+1<=m)dp[k+1][i+1][j+1]+=dp[k][i][j]*(n-i)*(m-j)/(n*m-k);
    				}
    		double ans=0;
    		for(int i=1;i<=n*m;i++)
    			ans+=dp[i][n][m]*i;
    		printf("%.12lf
    ",ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    VBA.replace替换单引号或双引号
    读取文件
    UPDATE
    alter update
    SQL日期格式
    python map的用法
    python os模块用法
    python re.I compile search
    python 正则匹配
    通过list中值得名称查询索引号
  • 原文地址:https://www.cnblogs.com/shaokele/p/8849396.html
Copyright © 2020-2023  润新知