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. Android开发之布局的学习

    FrameLayout-帧布局 实现效果: 代码: <?xml version="1.0" encoding="utf-8"?> <Frame ...

  2. Linux命令行批量替换多文件中的字符串【转】

    Linux命令行批量替换多文件中的字符串[转自百度文库] 一种是Mahuinan法,一种是Sumly法,一种是30T法分别如下: 一.Mahuinan法: 用sed命令可以批量替换多个文件中的字符串. ...

  3. 函数 buf_chunk_init

    http://www.tuicool.com/articles/3QbYfm http://www.360doc.com/content/13/1216/17/12904276_337644353.s ...

  4. poj3254

    还是那句老话:dp关键在状态: 求有多少种排布方式,是任意两头牛不相邻(有些地方不能放): 不用心,一开始还纠结了半天 和之前USACO上某题方法是一样的,每一行放或不放只有两种情况 把它当作一个二进 ...

  5. ASP.NET Identity(处理身份数据存储) 与 OWIN主机(实现katana验证授权)原理概括

    ASP.NET Identity 是4.5中引入的,支持Clamis(声明)式样登陆[即认证和授权分开模式],结合owin可以实现cookie加密等功能. 1.ASP.NET Identity架构框架 ...

  6. [swustoj 1088] 德州扑克

    德州扑克(1088) 问题描述 德州扑克是一款风靡全球的扑克游戏.德州扑克一共有52张牌,没有王牌.每个玩家分两张牌作为“底牌”,五张由荷官陆续朝上发出的作为公共牌.开始的时候,每个玩家会有两张面朝下 ...

  7. UpYun上传 401 Unauthorized

    _upt=3b9b444a14059041252014-07-21 08:46:25,218 ERROR (com.UpYun:518) - Upload file error:<h1>4 ...

  8. process thread Fiber(linux)

    http://blog.chinaunix.net/uid-21084809-id-2215376.html Processes, kernel threads, user threads, and ...

  9. 4、什么构成了我们Android应用程序?(七大件)

    一.应用程序四大组件 [Activity] Activity是Android应用程序的一个界面,可以通过这个界面查看联系人,打电话戒玩游戏. b. 一个应用程序通常包含多个Activity. c. A ...

  10. Jenkin+TestNG进行自动化测试执行

    1.登陆jenkins'后,主页面有一个jenkins管理选项,进入该模块,对插件,系统进行配置.(安装一个extend choice parameter插件) 2.点击new item新建一个项目, ...