zoj 3822 Domination (可能性DP)
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 Mcolumns.
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
题意:向一个N*M的棋盘里随机放棋子,每天往一个格子里放一个。求每一行每一列都有棋子覆盖的天数。
思路:开一个三维数组,dp[i][j][k]:有i行j列被k个棋子覆盖的概率。
则dp[i+1][j][k+1]=dp[i][j][k]*(n-i)*j/(n*m-k);
//添加一个棋子,多覆盖一行
dp[i][j+1][k+1]=dp[i][j][k]*i*(m-j)/(n*m-k);
//添加一个棋子,多覆盖一列
dp[i+1][j+1][k+1]=dp[i][j][k]*(n-i)*(m-j)/(n*m-k);
//添加一个棋子,多覆盖一行及一列
dp[i][j][k+1]=dp[i][j][k]*(i*j-k)/(n*m-k);
//添加一个棋子,行、列数没有添加
则ans=dp[n][m][k]*k,(k=0...n*m).
//当i==n&&j==m时特殊处理,最后一项去掉。
易知dp[0][0][0]=1;
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 55
#define LL __int64
const int inf=0x1f1f1f1f;
const double eps=1e-10;
double dp[N][N][N*N];
int n,m;
void inti()
{
int i,j,k;
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
for(k=0;k<=n*m;k++)
dp[i][j][k]=0;
}
}
}
int main()
{
int i,j,k,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
inti();
dp[0][0][0]=1;
int tt=n*m;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
for(k=0;k<=n*m;k++)
{
if(i==n&&j==m)
dp[i][j][k]=(dp[i-1][j][k-1]*(n-i+1)*j+dp[i][j-1][k-1]*i*(m-j+1)+dp[i-1][j-1][k-1]*(n-i+1)*(m-j+1))/(tt-k+1);
else
dp[i][j][k]=(dp[i-1][j][k-1]*(n-i+1)*j+dp[i][j-1][k-1]*i*(m-j+1)+dp[i-1][j-1][k-1]*(n-i+1)*(m-j+1)+dp[i][j][k-1]*(i*j-k+1))/(tt-k+1);
}
}
}
double ans=0;
for(i=0;i<=tt;i++)
ans+=dp[n][m][i]*i;
printf("%.9f\n",ans);
}
return 0;
}
zoj 3822 Domination (可能性DP)的更多相关文章
- ZOJ 3822 Domination 期望dp
Domination Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...
- ZOJ 3822 Domination 概率dp 难度:0
Domination Time Limit: 8 Seconds Memory Limit: 131072 KB Special Judge Edward is the headm ...
- zoj 3822 Domination 概率dp 2014牡丹江站D题
Domination Time Limit: 8 Seconds Memory Limit: 131072 KB Special Judge Edward is the headm ...
- zoj 3822 Domination (概率dp 天数期望)
题目链接 参考博客:http://blog.csdn.net/napoleon_acm/article/details/40020297 题意:给定n*m的空棋盘 每一次在上面选择一个空的位置放置一枚 ...
- ZOJ 3822 Domination(概率dp)
一个n行m列的棋盘,每天可以放一个棋子,问要使得棋盘的每行每列都至少有一个棋子 需要的放棋子天数的期望. dp[i][j][k]表示用了k天棋子共能占领棋盘的i行j列的概率. 他的放置策略是,每放一次 ...
- ZOJ 3822 Domination(概率dp 牡丹江现场赛)
题目链接:problemId=5376">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376 Edward ...
- ZOJ - 3822 Domination (DP)
Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess ...
- zoj 3822 Domination(dp)
题目链接:zoj 3822 Domination 题目大意:给定一个N∗M的棋盘,每次任选一个位置放置一枚棋子,直到每行每列上都至少有一枚棋子,问放置棋子个数的期望. 解题思路:大白书上概率那一张有一 ...
- zoj 3822(概率dp)
ZOJ Problem Set - 3822 Domination Time Limit: 8 Seconds Memory Limit: 131072 KB Special Ju ...
随机推荐
- 积累的VC编程小技巧之文件操作
1.删除文件夹 // 删除文件夹及其所有内容void CBaseDoc::RemoveFolder(const CString &strPathName){ CString path = ...
- 【ASP.NET Web API教程】5.4 ASP.NET Web API批处理器
原文:[ASP.NET Web API教程]5.4 ASP.NET Web API批处理器 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内容. ...
- mysql 主从同步出问题,重新修复从库 - web架构研究
mysql 主从同步出问题,重新修复从库 - web架构研究 mysql 主从同步出问题,重新修复从库 0 昨天由于操作失误,在从库上执行一堆sql之后,导致主从同步错误,并且已 ...
- hdu 4715 Difference Between Primes 2013年ICPC热身赛A题 素数水题
题意:给出一个偶数(不论正负),求出两个素数a,b,能够满足 a-b=x,素数在1e6以内. 只要用筛选法打出素数表,枚举查询下就行了. 我用set储存素数,然后遍历set里面的元素,查询+x后是否还 ...
- Repeater
- 运用mapreduce计算tf-idf
问题描写叙述:给定一个大文件,文件里的内容每一行为:文档名,文档内容. input 文档名1,word1 Word2 ....... 文档名2,word1 Word2 ....... output w ...
- objective-c 中数据类型之六 数值类(NSValue)
// NSValue能够将c类型转换为Objective-C对象,如NSRange,CGPoint.CGSize,CGRect,CGVector,UIEdgeInsets,UIOffset NSRan ...
- Android自己定义控件——3D画廊和图像矩阵
转载请注明出处:http://blog.csdn.net/allen315410/article/details/39932689 1.3D画廊的实现 我们知道android系统已经为我们提供好了一个 ...
- 在WPF的WebBrowser控件中抑制脚本错误
原文:在WPF的WebBrowser控件中抑制脚本错误 今天用WPF的WebBrowser控件的时候,发现其竟然没有ScriptErrorsSuppressed属性,导致其到处乱弹脚本错误的对话框,在 ...
- 小心LinkedHashMap的get()方法(转)
这是一个来自实际项目的例子,在这个案例中,有同事基于jdk中的LinkedHashMap设计了一个LRUCache,为了提高性能,使用了 ReentrantReadWriteLock 读写锁:写锁对应 ...