Domination

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3822

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 <= 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

HINT

题意

每次这个人会随机选择一个空格子扔棋子,然后问你期望扔多少次,可以把n*m的矩阵,每一行和每一列都至少有一个棋子

题解:

期望dp,用容斥做

dp[i][j][k]表示占领了i行j列,用了k个

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1) using namespace std;
const int maxn = + ;
double dp[maxn][maxn][maxn*maxn];
int n , m ; inline double GetDouble(int x)
{
return (double)x;
} void initiation()
{
memset(dp,-,sizeof(dp));
scanf("%d%d",&n,&m);
} double dfs(int x,int y,int k)
{
if(dp[x][y][k]> -0.5) return dp[x][y][k];
double & ans = dp[x][y][k] = ;
if(x == n && y == m ) return ans;
int all = m*n-k;
if(x*y != k) ans += dfs(x,y,k+)*GetDouble(x*y-k)/GetDouble(all);
if(x != n && y != m) ans += dfs(x+,y+,k+)*GetDouble((n-x)*(m-y))/GetDouble(all);
if(x != n && y != ) ans += dfs(x+,y,k+)*GetDouble(y*(n-x))/GetDouble(all);
if(y != m && x != ) ans += dfs(x,y+,k+)*GetDouble(x*(m-y))/GetDouble(all);
ans += ;
return ans;
} double solve()
{
return dfs(,,);
} int main(int argc,char *argv[])
{
int Case;
scanf("%d",&Case);
while(Case--)
{
initiation();
printf("%.12lf\n",solve());
}
return ;
}

ZOJ 3822 Domination 期望dp的更多相关文章

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

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

  2. zoj 3822 Domination 概率dp 2014牡丹江站D题

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

  3. ZOJ 3822 Domination(概率dp)

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

  4. zoj 3822 Domination (可能性DP)

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

  5. ZOJ - 3822 Domination (DP)

    Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess ...

  6. ZOJ 3822 Domination 概率dp 难度:0

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

  7. ZOJ 3822 Domination(概率dp 牡丹江现场赛)

    题目链接:problemId=5376">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376 Edward ...

  8. zoj 3822 Domination(dp)

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

  9. zoj 3822(概率dp)

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

随机推荐

  1. UVa 129 (回溯法) Krypton Factor

    回溯法确实不是很好理解掌握的,学习紫书的代码细细体会. #include <cstdio> ]; int n, L, cnt; int dfs(int cur) { if(cnt++ == ...

  2. 【众秒之门 JavaScript与jQuery技术精粹 #BOOK#】第4章 数据类型及定义

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  3. 【转】Bluetooth数据包捕获

    原文网址:http://www.cnblogs.com/hzl6255/p/3887013.html 这里介绍一种在Android上捕获蓝牙数据包的方法 1. 前提 首先你要有一部Android手机  ...

  4. JPA--联合主键

    联合主键的一些知识: 使用@EmbeddedId标示联合主键: 在联合主键类中只是定义确定联合主键的字段即可: * 联合主键类的规则 * 1.必须包含一个无参的构造函数 * 2.必须实现序列化接口 * ...

  5. POJ 1258 Agri-Net

    题意:给一个无向图的邻接矩阵,求最小生成树. 解法:Kruskal算法.把边按边权排序,从小到大插入生成树中,如果一个边的两个点都在生成树中则不插入,用并查集维护. 代码: #include<s ...

  6. 【Python】使用python的tornado配合html页面示例

    背景:java写的非标加密算法,测试时执行java工程进行解密测试,很不方便. 目的:想写个web页面,使得任何测试人员都可以在输入加密串时得到解密后字段,方便日志查询及字段核对.(额,算法部分就不写 ...

  7. [OFBiz]简介 一

    1.What is Apache OFBiz?http://ofbiz.apache.org/ 2.概述http://baike.baidu.com/view/638900.html?fromTagl ...

  8. [selenium webdriver Java]使用自定义条件同步测试

    Selenium WebDriver可以结合ExpectedCondition类来定义自己期望的条件 创建一个新的ExpectedCondition接口,必须实现apply方法 等待元素出现 publ ...

  9. linux 打开远程samba服务器

    sudo mount -t cifs //10.104.*.*data /home/leeyoung/samba/ -o username=123,password=123

  10. Hapoop原理及MapReduce原理分析

    Hapoop原理 Hadoop是一个开源的可运行于大规模集群上的分布式并行编程框架,其最核心的设计包括:MapReduce和HDFS.基于 Hadoop,你可以轻松地编写可处理海量数据的分布式并行程序 ...