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;
    }
    
  • 相关阅读:
    第3章 Spring AOP
    第2章 Spring中的Bean
    第1章 Spring的应用
    Codeforces Round #558 (Div. 2)-Cat Party (Hard Edition)-(前缀和 + 模拟)
    Codeforces Round #552 (Div. 3)-1154E-Two Teams-(模拟+双指针)
    Codeforces Round #552 (Div. 3)-D-Walking Robot-(贪心)
    Codeforces Round #552 (Div. 3)-C-Gourmet Cat
    Codeforces Round #555 (Div. 3)
    2019年湘潭大学程序设计竞赛(重现赛)
    Buy Fruits-(构造)
  • 原文地址:https://www.cnblogs.com/shaokele/p/8849396.html
Copyright © 2011-2022 走看看