zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    20190503-汉明距离
    20190501-编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串
    20190502-罗马数字转换为数字
    20190501-整数翻转
    20190426-选择排序算法
    Excel技巧—一个公式实现中英文翻译
    Excel技巧—两招轻松搞定汉字转拼音
    Excel基础—开始菜单之花式粘贴四
    Excel技巧—瞬间吸引眼球的WIFI图表
    Excel技巧—自动标记颜色条件格式的妙用
  • 原文地址:https://www.cnblogs.com/shaokele/p/8849396.html
Copyright © 2011-2022 走看看