ZOJ 3875 Domination (概率DP)
来源:互联网 发布:调度算法有哪些 编辑:程序博客网 时间:2024/06/07 17:34
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 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
21 32 2
Sample Output
3.0000000000002.666666666667
题意:N*M的格子里放棋子,问期望
分析:其实这道概率非常水,开始设dp[x][y]:放了x行y列,发现不行,样列都出不来,
仔细分析下发现其实在x*y的面积放是有限制的,所以要加一维,其他的就非常水了
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long ll;const int mod=10007;double dp[55][55][2550];int n,m;int t,S;double dfs(int x,int y,int s){ if(s>=S) return 0.0; if(x>=n&&y>=m) return 0.0; if(dp[x][y][s]!=-1.0) return dp[x][y][s]; double res=0; if(x<n) res+=(n-x)*y*dfs(x+1,y,s+1); if(y<m) res+=x*(m-y)*dfs(x,y+1,s+1); if(x<n&&y<m) res+=(n-x)*(m-y)*dfs(x+1,y+1,s+1); if(s<x*y) res+=(x*y-s)*dfs(x,y,s+1); return dp[x][y][s]=(res+S-s)/(S-s);}int main(){ scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); S=n*m; for(int i=0;i<=n;i++) for(int j=0;j<=m;j++) for(int k=0;k<=S;k++) dp[i][j][k]=-1.0; double ans=dfs(0,0,0); printf("%.10f\n",ans); } return 0;}
- ZOJ 3875 Domination (概率DP)
- ZOJ 3822 Domination (概率DP)
- ZOJ - 3822 Domination (概率DP)
- ZOJ 3822 Domination(概率DP)
- [概率dp] zoj 3822 Domination
- ZOJ 3822 Domination(概率dp)
- zoj 3288 Domination (概率dp)
- [概率dp] ZOJ 3822 Domination
- ZOJ 3822 Domination(概率dp)
- ZOJ 3822 Domination 概率DP
- zoj 3822 Domination 概率dp
- zoj 3822 Domination (概率DP)
- ZOJ - 3822 Domination(概率dp)
- ZOJ 3822 Domination 概率DP
- zoj 3822 Domination 概率dp
- ZOJ 3822 Domination [概率DP]
- zoj 3822 Domination 概率DP
- ZOJ 3822Domination(概率DP)
- python的测试框架(一) -- 单元测试框架
- SLURM Array Job
- Java并发编程(Callable、Future和CompletionService)
- 读 《一种更清晰的Android架构》笔记
- linux入门之echo
- ZOJ 3875 Domination (概率DP)
- linux下安装jdk配置环境变量
- TCP/IP详解-IP和IP选路
- Erlang可以挂掉的四种原因
- Android实战简易教程-第五十二枪(Fragment和Activity之间通信)
- 通信协议HTTP、TCP、UDP
- IOS隐式动画
- 【java基础】——线程
- ExpandListView使用自定义对象时异常