「Codeforces 148D」Bag of mice

袋子里有 \(w\) 只白鼠和 \(b\) 只黑鼠 ,\(A\) 和 \(B\) 轮流从袋子里抓,谁先抓到白色谁就赢。\(A\) 每次随机抓一只,\(B\) 每次随机抓完一只之后会有另一只随机老鼠跑出来。如果两个人都没有抓到白色则 \(B\) 赢。\(A\) 先抓,问 \(A\) 赢的概率。

Luogu

分析

不会概率 DP 。。。。。使用记忆化搜索。

我们记 \(f[i][j]\) 为当前还剩 \(i\) 个白鼠 \(j\) 个黑鼠 \(A\) 赢的概率。

\(dfs(n, m)\) , \(n\) 为当前白鼠个数, \(m\) 为当前黑鼠个数,如果 \(n=0\) ,那么显然 \(A\) 必输,否则如果 \(m=0\) ,那么 \(A\) 必赢。然后我们分情况讨论。先计算 \(A\) 抽到白鼠的概率为 \(\frac{n}{n+m}\) ,然后考虑 \(A\) 抽到黑鼠的情况。首先, \(A\) 抽到黑鼠的概率为 \(\frac{m}{n+m}\) ,如果 \(B\) 抽到白鼠,那么 \(A\) 赢的概率为 \(0\) ,而我们只需要计算 \(A\) 赢的概率,所以可以不用考虑 \(B\) 抽到白鼠。 \(B\) 抽到黑鼠的概率为 \(\frac{m-1}{n+m-1}\) ,然后我们接着考虑跑掉的老鼠,它是黑鼠的概率为 \(\frac{m-2}{n+m-2}\) ,是白鼠的概率为 \(\frac{n}{n+m-2}\) ,再分别乘上 \(dfs(n, m-3)\) 和 \(dfs(n-1, m-2)\) ,注意到如果当前只有两只黑鼠,那么跑掉的老鼠只能是白鼠,我们判断一波就好了。

代码

#include <bits/stdc++.h>

#define N 1003

using namespace std;

int gi() {
    int x = 0, f = 1; char c = getchar();
    for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
    for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
    return x * f;
}

double f[N][N];

double dfs(int n, int m) {
    if (!n) return 0.0;
    if (!m) return 1.0;
    if (f[n][m]) return f[n][m];
    double ans = 1.0 * n / (n + m);
    if (m == 2) ans += 1.0 * m / (n + m) * 1.0 * (m - 1) / (n + m - 1) * dfs(n - 1, m - 2);
    else if (m > 2) {
        double tmp = 1.0 * n / (n + m - 2) * dfs(n - 1, m - 2) + 1.0 * (m - 2) / (n + m - 2) * dfs(n, m - 3);
        ans += 1.0 * m / (n + m) * (m - 1) / (n + m - 1) * tmp;
    }
    return f[n][m] = ans;
}

int main() {
    int n = gi(), m = gi();
    printf("%.9f", dfs(n, m));
    return 0;
}

原文地址:https://www.cnblogs.com/hlw1/p/12273954.html

时间: 2024-08-04 05:24:23

「Codeforces 148D」Bag of mice的相关文章

【CodeForces】【148D】Bag of mice

概率DP kuangbin总结中的第9题 啊……题目给的数据只有白鼠和黑鼠的数量,所以我们只能在这个上面做(gao)文(D)章(P)了…… 明显可以用两种老鼠的数量来作为状态= = 我的WA做法: 令f[i][j]表示从(w,b)轮流取老鼠一直到(i,j)[此时轮process取]两人一直不分胜负的概率,很明显(i,j)这个状态下赢的概率为 i / (i+j) ,那么总共赢的概率就是对所有(i,j),f[i][j]*i/(i+j)求和 转移当然很简单啦~ f[i][j]只可能从f[i][j+3]

【Codeforces 105D】 Bag of mice

[题目链接] http://codeforces.com/contest/148/problem/D [算法] 概率DP f[w][b]表示还剩w只白老鼠,b只黑老鼠,公主胜利的概率,那么 : 1. 公主抓到白老鼠,概率为w/(w+b) 2. 公主抓到黑老鼠,那么龙也抓到黑老鼠,如果跑出来的老鼠是白颜色的,则概率为 : b / (w + b) * b / (w + b - 1) * w / (w + b - 2) * f[w-1][b-2] 否则,概率为 : b / (w + b) * (b -

Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题

除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃圾,大哥拿来了一袋老鼠,其中有w只白老鼠和b只黑老鼠.胡小兔先抓,先抓到白老鼠的人赢. 每次学姐抓完老鼠之后,总会有另外一只老鼠从袋子里自己跑出来(这只老鼠不算任何人抓的),而胡小兔抓老鼠时则不会发生这样的事. 每次袋子里的每只老鼠被抓到的概率相等,当有一只老鼠跑出来的时候,每只老鼠跑出来的几率也相

Codeforces 148D Bag of mice (概率dp)

D. Bag of mice time limit per test:2 seconds memory limit per test:256 megabytes The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, wh

CodeForces 148D. Bag of mice(概率dp啊 )

题目链接:http://codeforces.com/problemset/problem/148/D D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The dragon and the princess are arguing about what to do on the New Year'

cf 148D Bag of mice

The dragon 选一只老鼠,然后会跑掉一只 the princess选一只老鼠,不会跑出另外的老鼠 求the princess赢的概率 1 #include<iostream> 2 #include<string> 3 #include<cstdio> 4 #include<vector> 5 #include<queue> 6 #include<stack> 7 #include<algorithm> 8 #inc

loj #6091. 「Codeforces Round #418」幻想特快

#6091. 「Codeforces Round #418」幻想特快 1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 using namespace std; 7 #define maxn 10000 8 int n,a[maxn],b[maxn],p[maxn],nxt1,nxt2,tot,

CF 148d Bag of mice 概率DP 好题

D. Bag of mice The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are

Bag of mice(概率DP)

Bag of mice  CodeForces - 148D The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed