解题报告 之 ZOJ 3822 Domination
来源:互联网 发布:nginx 快速配置 编辑:程序博客网 时间:2024/06/10 03:37
解题报告 之 ZOJ 3822 Domination
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 <= 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
21 32 2
Sample Output
3.0000000000002.666666666667
#include <iostream>#include <cmath>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 50 + 10;double dp[MAXN][MAXN][MAXN*MAXN];int main(){int T;scanf( "%d", &T );while(T--){memset( dp, 0, sizeof dp );int n, m;scanf( "%d%d", &n, &m );for(int i = n; i >= 0; i--){for(int j = m; j >= 0; j--){if(i == n&&j == m) continue; //因为没有状态会转移到这个状态,否则会出现除零错误for(int k = i*j; k >= max( i, j ); k--) //注意细节,k可能的范围就是 i*j 到 max(i,j)。{//如果使用了超过i*j个棋子则不可能只有i行、j列有棋子。//如果连max(i,j)个棋子都没有,则不可能占i行或j列//注意先算概率,再乘dp,不然可能会爆。算概率的时候注意已经放过的地方要去除,所以-kdp[i][j][k] += (double)j*(n - i) / (n*m - k)*dp[i + 1][j][k + 1];dp[i][j][k] += (double)i*(m - j) / (n*m - k)*dp[i][j + 1][k + 1];dp[i][j][k] += (double)(i*j - k) / (n*m - k)*dp[i][j][k + 1]; //这里分子-k的原因是只能从这i*j的矩形中去选,所以要去除已经有棋子的格子dp[i][j][k] += (double)(n - i)*(m - j) / (n*m - k)*dp[i + 1][j + 1][k + 1];dp[i][j][k] += 1.0;//别忘了步骤加1}}}printf( "%.12lf\n", dp[0][0][0] ); //输出初态到终态所需要的步骤}return 0;}
- 解题报告 之 ZOJ 3822 Domination
- zoj 3822 Domination(dp)
- zoj 3822 Domination
- ZOJ 3822 Domination
- ZOJ 3822 Domination
- ZOJ 3822 Domination(DP)
- ZOJ 3822 Domination
- ZOJ 3822 Domination
- zoj 3822 Domination
- ZOJ 3822 Domination
- ZOJ 3822 Domination
- [概率dp] zoj 3822 Domination
- ZOJ 3822 Domination(概率dp)
- ZOJ - 3822 Domination (DP)
- [概率dp] ZOJ 3822 Domination
- ZOJ 3822 Domination(概率dp)
- ZOJ 3822 Domination 概率DP
- zoj 3822 Domination 概率dp
- DEDE织梦常用的调用方法大全
- DeCAF: A Deep Convolutional Activation Featurefor Generic Visual Recognition阅读报告(1)
- 软件测试 读书笔记(思维导图)
- DeCAF: A Deep Convolutional Activation Featurefor Generic Visual Recognition阅读报告(2)
- Z-stack 2.6.1中新增的ZIGBEE_CHILD_AGING模式怎么使用?
- 解题报告 之 ZOJ 3822 Domination
- java面试题三十一
- 简单的数字顺序表游戏
- spring笔记 Bean实例化的机制
- 数据库连接池库libzdb使用教程
- 【SSH快速进阶】——Hibernate环境搭建
- Enable Nux Dextop Repository on CentOS
- Android适配器进阶之三(抽象分类适配器)
- Android学习路线图