Domination


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

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 <= 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
/**
题意:如题
做法:dp dp[i][j][k] 表示下第i枚棋 在j,k的位置
**/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <stdio.h>
using namespace std;
#define maxn 60
double dp[maxn * maxn][maxn][maxn];
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n, m;
memset(dp, , sizeof(dp));
scanf("%d %d", &n, &m);
dp[][][] = 1.0;
for(int i = ; i < n * m; i++)
{
for(int j = ; j <= n; j++)
{
for(int k = ; k <= m; k++)
{
dp[i + ][j + ][k + ] += dp[i][j][k] * (n - j) * (m - k) / (n * m - i);
dp[i + ][j][k + ] += dp[i][j][k] * j * (m - k) / (n * m - i);
dp[i + ][j + ][k] += dp[i][j][k] * (n - j) * k / (n * m - i);
dp[i + ][j][k] += dp[i][j][k] * (k * j - i) / (n * m - i);
}
}
}
double ans = ;
for(int i = ; i <= n * m; i++)
{
ans += i * (dp[i][n][m] - dp[i - ][n][m]);
}
printf("%.10f\n", ans);
}
return ;
}

ZOJ-3822的更多相关文章

  1. zoj 3822 Domination(dp)

    题目链接:zoj 3822 Domination 题目大意:给定一个N∗M的棋盘,每次任选一个位置放置一枚棋子,直到每行每列上都至少有一枚棋子,问放置棋子个数的期望. 解题思路:大白书上概率那一张有一 ...

  2. zoj 3822(概率dp)

    ZOJ Problem Set - 3822 Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Ju ...

  3. ZOJ 3822 Domination 期望dp

    Domination Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...

  4. ZOJ 3822 可能性DP

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3822 本场比赛之前,我记得.见WALK概率路DP称号.那么它应该是可以考虑 ...

  5. zoj 3822 Domination(2014牡丹江区域赛D题) (概率dp)

    3799567 2014-10-14 10:13:59                                                                     Acce ...

  6. ZOJ 3822(求期望)

    Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headm ...

  7. zoj 3822 Domination (概率dp 天数期望)

    题目链接 参考博客:http://blog.csdn.net/napoleon_acm/article/details/40020297 题意:给定n*m的空棋盘 每一次在上面选择一个空的位置放置一枚 ...

  8. ZOJ 3822 Domination(概率dp)

    一个n行m列的棋盘,每天可以放一个棋子,问要使得棋盘的每行每列都至少有一个棋子 需要的放棋子天数的期望. dp[i][j][k]表示用了k天棋子共能占领棋盘的i行j列的概率. 他的放置策略是,每放一次 ...

  9. ZOJ 3822 Domination

    题意: 一个棋盘假设每行每列都有棋子那么这个棋盘达到目标状态  如今随机放棋子  问达到目标状态的期望步数 思路: 用概率来做  计算第k步达到目标状态的概率  进而求期望  概率计算方法就是dp  ...

  10. [概率dp] ZOJ 3822 Domination

    题意: 给N×M的棋盘.每天随机找一个没放过棋子的格子放一个棋子 问使得每一个每列都有棋子的天数期望 思路: dp[i][j][k] 代表放了i个棋子占了j行k列 到达目标状态的期望 然后从 dp[n ...

随机推荐

  1. HDU 4468 Spy(KMP+贪心)(2012 Asia Chengdu Regional Contest)

    Description “Be subtle! Be subtle! And use your spies for every kind of business. ”― Sun Tzu“A spy w ...

  2. URAL 1936 Roshambo(求期望)

    Description Bootstrap: Wondering how it's played? Will: It's a game of deception. But your bet inclu ...

  3. centos7.4内核从3.10升级到4.14详细步骤

    由于我们的docker学习中的Overlay需要内核版本在3.12+,所以在安装完centos7.4之后要进行内核升级,下面是升级步骤:1.导入keyrpm --import https://www. ...

  4. winform 控件大小随着窗体自适应

    3个方法: #region 控件缩放变量        double formWidth;//窗体原始宽度        double formHeight;//窗体原始高度        doubl ...

  5. C#的internal访问修饰符

    文章:C# 访问修饰符internal的访问范围误区释疑 internal访问修饰符限定的类,只能在本程序集中访问.

  6. J2EE开发实战基础系列一 HelloWorld

    开始咱们的第一个程序,首先是配置环境,按照上一章所描述的方式下载开发工具,然后配置Java环境变量,给大家看下具体的结构: 环境变量配置OK的提示,如上图. Eclipse和Tomcat的文件目录位置 ...

  7. 并发(一) Semaphore

    Semaphore 控制对资源的并发访问数,构造时如果传参为1,则近似于ReentrantLock,差别在于锁的释放.可以一个线程获取锁,另外一个线程释放锁,在一些死锁处理的场合比较适用. 如上所示, ...

  8. Elasticsearch1.x 和Elasticsearch2.x 拼音分词插件lc-pinyin安装教程

    Elasticsearch1.x 基于lc-pinyin和ik分词实现 中文.拼音.同义词搜索 https://blog.csdn.net/chennanymy/article/category/60 ...

  9. BZOJ4373 算术天才⑨与等差数列(线段树)

    看上去很难维护,考虑找一些必要条件.首先显然最大值-最小值=k*(r-l).然后区间内的数需要模k同余.最后区间内的数两两不同(k=0除外).冷静一下可以发现这些条件组合起来就是充分的了. 考虑怎么维 ...

  10. 魔法物品(magic.pas/c/cpp)

    有两种类型的物品:普通物品和魔法物品.普通物品没有魔法属性,而魔法物品拥有一些魔法属性.每种普通物品有一个价值P,但每种魔法物品有两种价值:鉴定前的价值P.和鉴定后的价值P2(当然,P2总是大于P.) ...