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 ...
随机推荐
- Documentation | AnsibleWorks
Documentation | AnsibleWorks Welcome to the Ansible documentation! Ansible is an IT automation too ...
- 简单的ajax获取json
一个DBhelper类,用来操作数据库 using System; using System.Collections.Generic; using System.Linq; using System. ...
- Delphi事件的广播 good
明天就是五一节了,辛苦了好几个月,借此机会应该尽情放松一番.可是想到Blog好久没有写文章,似乎缺些什么似的.这几个月来在项目中又增长了许多经验,学到许多实际应用的知识.不如把一些比较有用的记录下来, ...
- 提高mysql memory(heap) engine内存性能的开源补丁_XMPP Jabber即时通讯开发实践_百度空间
提高mysql memory(heap) engine内存性能的开源补丁_XMPP Jabber即时通讯开发实践_百度空间 提高mysql memory(heap) engine内存性能的开源补丁
- VK Cup 2012 Qualification Round 1---C. Cd and pwd commands
Cd and pwd commands time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- 忽然想到:QProcess的Read功能太强,如果有什么搞不定的,可以调用外部程序 good
这样就可以用其它语言来无限扩展它的功能了,比如golang,比如Delphi
- drupal THEME主要文件
**.info 文件** .info 文件是一个必需的文件:Drupal 必须包括它,才干看到主题. .info 文件告诉 Drupal 主题的内部名称.比如,假设这个文件的名称是 ibmtheme. ...
- webmagic加上了注解支持
今天有个网友在博客回帖,能不能用注解来写一个爬虫?想了想,因为Javaer总习惯结果有个对象Model(我在自己用的时候也是这样),ResultItems的key-value形式难免会有点麻烦,何不将 ...
- Python语言总结 4.2. 和字符串(str,unicode等)处理有关的函数
4.2.7. 去除控制字符:removeCtlChr Python语言总结4.2. 和字符串(str,unicode等)处理有关的函数Sidebar Prev | Up | Next4.2.7 ...
- swfupload组件后台获取中文文件名称乱码的问题解决
问题描写叙述:用swfupload上传文件,含有中文名称的文件上传会报错,我用的是获取FileItem对象,用FileItem对象的getName()方法获取文件名会乱码,试着用request. ...