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 withN rows andM columns.

Every day after work, Edward will place a chess piece on a random empty cell. A few days later, he found the chessboard wasdominated 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 ofN ×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,做这道题时只推出了4个状态方程,然后就卡壳了o(╯□╰)o

毕竟看的别人题解,感觉写的很好。给个链接:题解点我

看来以后该刷概率DP了。

AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
double dp[2501][51][51];//dp[k][i][j] 表示放k个棋子 占i行 和 j列的概率
int main()
{
    int t;
    int N, M;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &N, &M);
        memset(dp, 0, sizeof(dp));
        dp[0][0][0] = 1;
        double p;
        for(int k = 0; k <= N*M; k++)
        {
            for(int i = 0; i <= N; i++)
            {
                for(int j = 0; j <= M; j++)
                {
                    //k个棋子 占i行 j列 概率为0不考虑
                    //k个棋子占了N行 M列  不再继续放棋子 也不考虑
                    if(i == N && j == M || dp[k][i][j] == 0) continue;
                    //再放一个棋子 满足占i行 j列
                    //概率为 (i * j - k) / (N * M - k)
                    if(i * j >= k)
                        dp[k+1][i][j] += dp[k][i][j] * (i * j - k) / (N * M - k);
                    //再放一个棋子 满足占i+1行 j列
                    //概率为 (N - i) * j / (N * M - k)
                    if(i < N)
                        dp[k+1][i+1][j] += dp[k][i][j] * (N - i) * j / (N * M - k);
                    //再放一个棋子 满足占i行 j+1列
                    //概率为 (M - j) * i / (N * M - k)
                    if(j < M)
                        dp[k+1][i][j+1] += dp[k][i][j] * (M - j) * i / (N * M - k);
                    //再放一个棋子 满足占i+1行 j+1列
                    //概率为 (N - i) * (M - j) / (N * M - k)
                    if(i < N && j < M)
                        dp[k+1][i+1][j+1] += dp[k][i][j] * (N - i) * (M - j) / (N * M - k);
                }
            }
        }
        double ans = 0;
        for(int i = 0; i <= N*M; i++)//求期望
            ans += i * dp[i][N][M];
        printf("%.12lf\n", ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-17 08:19:18

zoj 3822 Domination 【概率DP 求期望】的相关文章

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

HDU 4405 Aeroplane chess (概率DP求期望)

题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点需要步数的期望 其中有m个跳跃a,b表示走到a点可以直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点可以到走到i+1,i+2,i+3,i+4,i+5,i+6 点且每个点的概率都为1/6 所以dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])/6  + 1(步数加一). 而对于有跳跃的点直接为dp[a]=dp[b]; #include<stdio.h>

HDU4336-Card Collector(概率DP求期望)

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2195    Accepted Submission(s): 1034 Special Judge Problem Description In your childhood, do you crazy for collecting the beautifu

HDU3853-LOOPS(概率DP求期望)

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 1864    Accepted Submission(s): 732 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help h

HDU4405-Aeroplane chess(概率DP求期望)

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1182    Accepted Submission(s): 802 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids lab

HDU 4050 wolf5x (概率DP 求期望)

题意:有N个格子,1~N,起点在0,每个格子有一个状态(0,1,2,3),每次可以跨[a,b]步, 问走完N个格子需要步数的期望,每次尽量走小的步数,即尽量走a步,不能则走a+1,-- 状态0意味着你不能踏进对应的网格. 状态1意味着你可以??步入网格用你的左腿. 状态2意味着你可以??步入网格用你的右腿. 状态3意味着你可以进入网格用任何你的腿,而接下来的步骤中,您可以使用任何的腿;即你不需要遵循上述规则. 思路:借鉴了各路大神的思想理解了下. dp[i][j] :表示走到第 i 个格子在 j

POJ 2096 Collecting Bugs(概率DP求期望)

传送门 Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 4333 Accepted: 2151 Case Time Limit: 2000MS Special Judge Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stu

Codeforces 235B Let&#39;s Play Osu! (概率dp求期望+公式变形)

B. Let's Play Osu! time limit per test:2 seconds memory limit per test:256 megabytes You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us deno