Domination(概率DP)
Domination
题目链接:https://odzkskevi.qnssl.com/9713ae1d3ff2cc043442f25e9a86814c?v=1531624384
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 Tindicating 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
dp[i][j][k] 代表的是k个棋已经占了i行j列,保证i行并且j列里面都会有一个棋
然后我们想下怎么推导呢想一下,我们每多下一个棋,有四种可能
第一种 多占了一列
第二种 多占了一行
第三种 下在对角线,占一行一列
第四种 下在了之前已经占过的行列上
就可以推导出式子dp[i][j][k]=dp[i][j-1][k-1]+dp[i-1][j][k-1]+dp[i-1][j-1][k-1]+dp[i][j][k-1];
这四种可能的和,但是我下棋的时候每个情况有多少个位置可以下,这里也要求下概率
占一行(意思是这个棋的列要下在之前占过的列的位置,因为要新占一行,所以选的行就是剩下没被占过的数量,然后再除以总的剩下可以下的位置)
其他的以此类推
然后dp数组就存的是概率,我们再去枚举那个占了整个棋盘的概率dp[n][m][i]*i即可
大佬博客:https://blog.csdn.net/cq_pf/article/details/48393897
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double dp[][][*];
int n,m;
void solve()
{
memset(dp,,sizeof(dp));
dp[][][]=1.0;
for(int i=;i<=n;i++)//枚举行
{
for(int j=;j<=m;j++)//枚举列
{
for(int k=;k<=n*m;k++)//枚举下的棋子个数
{
if(i==n&&j==m)//下最后一个棋的时候没有既不占列也不占行的可能性
{
dp[i][j][k]=(dp[i][j-][k-]*(m-j+)*i)/(n*m-k+)+(dp[i-][j][k-]*(n-i+)*j)/(n*m-k+)+
(dp[i-][j-][k-]*(m-j+)*(n-i+))/(n*m-k+);
}
else{
dp[i][j][k]=(dp[i][j-][k-]*(m-j+)*i)/(n*m-k+)+(dp[i-][j][k-]*(n-i+)*j)/(n*m-k+)+
(dp[i-][j-][k-]*(m-j+)*(n-i+))/(n*m-k+)+(dp[i][j][k-]*(j*i-(k-)))/(n*m-k+);
}
}
}
}
double sum=;
for(int i=;i<=n*m;i++)//算期望值
{
sum+=dp[n][m][i]*i;
}
printf("%.12lf\n",sum);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
solve();
}
}
Domination(概率DP)的更多相关文章
- 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 牡丹江现场赛)
题目链接:problemId=5376">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376 Edward ...
- ZOJ 3822 Domination(概率dp)
一个n行m列的棋盘,每天可以放一个棋子,问要使得棋盘的每行每列都至少有一个棋子 需要的放棋子天数的期望. dp[i][j][k]表示用了k天棋子共能占领棋盘的i行j列的概率. 他的放置策略是,每放一次 ...
- zoj3822 Domination 概率dp --- 2014 ACM-ICPC Asia Mudanjiang Regional Contest
一个n行m列的棋盘,每次能够放一个棋子.问要使得棋盘的每行每列都至少有一个棋子 须要的放棋子次数的期望. dp[i][j][k]表示用了k个棋子共能占据棋盘的i行j列的概率. 那么对于每一颗棋子,在现 ...
- ZOJ3822 ACM-ICPC 2014 亚洲杯赛事现场牡丹江司D称号Domination 可能性DP
Domination Time Limit: 8 Seconds Memory Limit: 131072 KB Special Judge Edward is the headm ...
- zoj 3822(概率dp)
ZOJ Problem Set - 3822 Domination Time Limit: 8 Seconds Memory Limit: 131072 KB Special Ju ...
- Codeforces 28C [概率DP]
/* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...
随机推荐
- G.711是一种由国际电信联盟(ITU-T)制定的音频编码方式
http://zh.wikipedia.org/zh-cn/G.711 ITU-T G.711 page ITU-T G.191 software tools for speech and audio ...
- python paramiko自动登录网络设备抓取配置信息
ssh = paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(hostn ...
- Vrrp和Hsrp的区别
VRRP原理协议简述简单来说,VRRP是一种容错协议,它为具有组播或广播能力的局域网(如以太网)设计,它保证当局域网内主机的下一跳路由器出现故障时,可以及时的由另一台路由器来代替,从而保持通讯的连续性 ...
- CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout的用法,让Toolbar与系统栏融为一体
CoordinatorLayout其实是加强版的FrameLayout布局,可以监听期所有子控件的各种事件,由Design Support库提供的,能体现Material Design 的魔力.能解决 ...
- 【CSS】【1】让DIV中的文字换行显示
<div style="white-space:normal;word-break:break-all;word-wrap:break-word;">data</ ...
- Spring Boot项目打包部署到外部Tomcat
1.生成war包 1)修改POM文件,将打包类型改为war:<packaging>war</packaging> <packaging>war</packag ...
- JQ 实现监测input中值的变化并绑定到另个input
$('#input').bind('input propertychange', function () { $('#myDiv ...
- 时间序列(六): 炙手可热的RNN: LSTM
目录 炙手可热的LSTM 引言 RNN的问题 恐怖的指数函数 梯度消失* 解决方案 LSTM 设计初衷 LSTM原理 门限控制* LSTM 的 BPTT 参考文献: 炙手可热的LSTM 引言 上一讲说 ...
- 【转】vue项目重构技术要点和总结
vue数据更新, 视图未更新 这个问题我们经常会遇到,一般是vue数据赋值的时候,vue数据变化了,但是视图没有更新.这个不算是项目重构的技术要点,也和大家分享一下vue2.0通常的解决方案吧! 解决 ...
- dvwa安装、配置、使用教程(Linux)
一.搭建LAMP环境 首先搭建好LAMP环境,如没配好参见“Linux+Apache+MySQL+PHP配置教程” 或者使用官方推荐的XAMPP:https://www.apachefriends.o ...