zoj3822 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 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

2
1 3
2 2

Sample Output

3.000000000000
2.666666666667题意:    说有个傻孩子喜欢痴迷下象棋,并且有一个习惯,每一天随机放一只棋子在(n*m)的棋盘的随意一个位子里。问当满足任意一行以及任意一列都有棋子时。  再不放了。问这样随机放棋子直到放不了棋子平均需要多少天.....

用DP【i】【j】【k】表示前i行有棋子,前j列有棋子,并且在放了k个棋子的情况下
的概率,对于DP【i】【j】【k】此刻的状态等于上一状态的和...... 推了很久也没有退出这个转移方程..看了一下解题报告,才发下逗比的忘记算dp[i-1][j-1][k-1]这一种情况...代码:

 1 //#define LOCAL
 2 #include<cstdio>
 3 #include<cstring>
 4 #define maxn 55
 5 double dp[maxn][maxn][maxn*maxn];
 6 int n,m;
 7 int main()
 8 {
 9    #ifdef LOCAL
10      freopen("test.in","r",stdin);
11    #endif
12     int cas,i,j,k;
13    scanf("%d",&cas);
14     while(cas--)
15     {
16      scanf("%d%d",&n,&m);
17      int tt=n*m;
18      memset(dp,0,sizeof(dp));
19      dp[0][0][0]=1;
20       for(i=1;i<=n;i++)
21       {
22           //表示前i行有旗子
23         for(j=1;j<=m;j++)
24         {
25           //表示前j列有棋子
26            for(k=1;k<=tt;k++)
27             {
28                //当n行m列中都有棋子时
29                 if(i==n&&j==m)
30                     dp[i][j][k]=(dp[i-1][j][k-1]*(n-i+1)*j+dp[i][j-1][k-1]*(m-j+1)*i+dp[i-1][j-1][k-1]*(n-i+1)*(m-j+1))/(tt-k+1);
31                 else
32                     dp[i][j][k]=(dp[i-1][j][k-1]*(n-i+1)*j+dp[i][j-1][k-1]*(m-j+1)*i+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);
33             }
34         }
35       }
36       double ans=0;
37       for(int i=0;i<=n*m;i++)
38          ans+=dp[n][m][i]*i;  //得到期望
39         printf("%.8lf\n",ans);
40     }
41    return 0;
42 }

时间: 2024-10-24 16:23:33

zoj3822 Domination(概率dp)的相关文章

zoj 3822 Domination 概率dp 2014牡丹江站D题

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 chessboar

zoj 3288 Domination (概率dp)

///dp[i][j][k]表示i行j列已经有棋子,且放了k个的概率 ///dp[i][j][k]一共有四种转移方式 ///1:dp[i-1][j][k-1] 概率为 (n-(i-1))*j/(n*m-(k-1)) ///2:dp[i][j-1][k-1] 概率为 i*(m-(j-1))/(n*m-(k-1)) ///3:dp[i-1][j-1][k-1] 概率为 (n-(i-1))*(m-(j-1))/(n*m-(k-1)) ///4:dp[i][j][k-1] 概率为 (i*j-(k-1))

ZOJ3822 ACM-ICPC 2014 亚洲区域赛牡丹江赛区现场赛D题Domination 概率DP(两种解法)

题目地址:点击打开链接 这道题有两种做法,第一种是直接求期望,类似于poj 2096 区别在于这个步数有限.所以要迭代步数. #include <cstdio> #include <cstring> #include <iostream> #define maxn 55//这里刚开始写成了50+10 那么maxn*maxn就会小很多wa了一次 using namespace std; double dp[maxn][maxn][maxn*maxn]; int N,M,T

ZOJ--3822(概率dp)

题意:一个n行m列的棋盘,每次可以放一个棋子,问要使得棋盘的每行每列都至少有一个棋子 需要的放棋子次数的期望. 思路: 定义三维的状态,dp[i][j][k]表示用k天占据了i行j列的概率. 下一天的概率分四种情况,一个是只占据了新的一行,只占据了新的一列,占据了新的一行和一列,并没有占据新的行和列. 初始化只用初始化dp[1][1][1]=1.0即可,第一个棋子放上肯定占据新的一行和一列,其它赋值为0即可. 求期望的时候要每次的概率要减去在k-1天就达到n*m的概率,再乘以天数,相加即可: 求

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 chessboar

ZOJ 3822 Domination 概率DP

题意:在一个n*m的棋盘上放棋子,一个棋子可覆盖一行一列,若n行m列全被覆盖则停止放棋子,求棋子的期望 思路:期望DP, dp[i][j][k]表示放了i个棋子覆盖了j行k列 已知dp[0][0][0]=1,求dp[1~n*m][n][m] 四种情况: 1.再放一个棋子,行列都不增加 dp[i+1][j][k]+=dp[i][j][k]*(j*k-i)*1.0/(m*n-i); 2.只增加一行 dp[i+1][j+1][k]+=dp[i][j][k]*(n-j)*k*1.0/(m*n-i); 3

[概率dp] zoj 3822 Domination

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3822 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 che

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 chessboar

概率DP入门题

一 概率问题的论文 1.算法合集之<信息学竞赛中概率问题求解初探> 2.有关概率和期望问题的研究 3.算法合集之<浅析竞赛中一类数学期望问题的解决方法> 二 入门题目 1.POJ 3744 Scout YYF I (简单题) 题意:一条路上有n个地雷 ,a[i]代表第i个地雷放的位置,求安全走过这段路的概率 分析:若第k个位置有地雷则安全走过这个位置的方案为在第k-1个位置跳两步概率为(1-p) 从反面考虑 已经安全走过了第i-1个雷 则在第i个雷的死掉的概率为 1-p(从走到a[

Codeforces 28C [概率DP]

/* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队伍的期望. 思路: 概率dp dp[i][j][k]代表前i个浴室有j个人最长队伍是k的概率. 枚举第i个浴室的人数.然后转移的时候其实是一个二项分布. */ #include<bits/stdc++.h> using namespace std; int jilu[55]; double dp[