zoj3822||牡丹江现场赛D题 概率dp

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376

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 <= NM <= 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
/**
zoj 3822
题意:问把一个n*m的棋盘填成每行至少一个棋子,每列至少一个棋子的状态,需要棋子的数学期望
解题思路: dp[i][j][k]表示到达结果状态时所需要的棋子数的期望值。那么dp[0][0][0]就是结果,状态转移方程见代码。
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int T,n,m;
double dp[55][55][2555];
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(dp,0,sizeof(dp));
        for(int i=n; i>=0; i--)
        {
            for(int j=m; j>=0; j--)
            {
                if(j==m&&n==i)
                    continue;
                for(int t=i*j; t>=max(i,j); t--)
                {
                    dp[i][j][t]+=dp[i][j][t+1]*1.0*(i*j-t)/(n*m-t);
                    dp[i][j][t]+=dp[i][j+1][t+1]*1.0*(m-j)*i/(n*m-t);
                    dp[i][j][t]+=dp[i+1][j][t+1]*(1.0)*(n-i)*j/(n*m-t);
                    dp[i][j][t]+=dp[i+1][j+1][t+1]*(1.0)*(n-i)*(m-j)/(n*m-t);
                    dp[i][j][t]+=1.0;
                }
            }
        }
        printf("%.12lf\n",dp[0][0][0]);
    }
    return 0;
}

时间: 2024-11-05 23:34:56

zoj3822||牡丹江现场赛D题 概率dp的相关文章

zoj 3819(牡丹江现场赛A题)

马上要去上海了,刷刷现场赛的题找找感觉~~~ 这题.......额,没什么好说的,太水.. ZOJ Problem Set - 3819 Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar University. He is clever and diligent. However, he is not good at math, especially in Mat

2014牡丹江区域赛D(概率DP)ZOJ3822

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

hdu5074 Hatsune Miku 2014鞍山现场赛E题 水dp

http://acm.hdu.edu.cn/showproblem.php?pid=5074 Hatsune Miku Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 325    Accepted Submission(s): 243 Problem Description Hatsune Miku is a popular vi

zoj 3827(牡丹江现场赛I题)

ZOJ Problem Set - 3827 Information Entropy Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Information Theory is one of the most popular courses in Marjar University. In this course, there is an important chapter about informatio

ZOJ 2819 Average Score 牡丹江现场赛A题 水题/签到题

ZOJ 2819 Average Score Time Limit: 2 Sec  Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5373 Description Bob is a freshman in Marjar University. He is clever and diligent. However, he is not good at math, especia

HDU 4815 2013长春现场赛C题

C - Little Tiger vs. Deep Monkey Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4815 Description A crowd of little animals is visiting a mysterious laboratory ? The Deep Lab of SYSU. "Are y

HDUOJ-------2493Timer(数学 2008北京现场赛H题)

Timer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 445    Accepted Submission(s): 90 Problem Description Recently, some archaeologists discovered an ancient relic on a small island in the Pa

HDU 4791 Alice&#39;s Print Service(2013长沙区域赛现场赛A题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4791 解题报告:打印店提供打印纸张服务,需要收取费用,输入格式是s1 p1 s2 p2 s3 p3...表示打印区间s1到s2张纸的单价是p1,打印区间s2 到s3的单价是p2....最后是sn到无穷大的单价是pn,让你求打印k张纸的总费用最少是多少?有m次查询. 因为s1*p1 > s2 * p2 > s3*p3......,很显然,加入k所在的那个区间是第x个区间,那么最低费用要么是k * p

2013 ACM/ICPC 长沙现场赛 A题 - Alice&#39;s Print Service (ZOJ 3726)

Alice's Print Service Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her print service found some tricks to save money. For example, the price when