惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
IT之家
IT之家
博客园_首页
量子位
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
博客园 - 聂微东
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
宝玉的分享
宝玉的分享
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
Codeforces 148D.Bag of mice|OhYee 博客
2017-08-03 · via OhYee 博客

这是一篇最后编辑于 8 年前 的文章,其内容可能与目前实际情况差异较大,请注意甄别

题目

{% fold 点击显/隐题目 %}

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 desperate to come to an amicable agreement, so they decide to leave this up to chance. They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning? If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one.

The only line of input data contains two integers w and b (0?≤?w,?b?≤?1000).

the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10?-?9. Example

1 3

{% endfold %}

题解

记忆化搜索即可
注意初始值以及除以0的情况

代码

{% fold 点击显/隐代码 %}```cpp Bag of mice https://github.com/OhYee/sourcecode/tree/master/ACM 代码备份
#include

const int maxn = 1005;
const double eps = 1e-12;

double dp[maxn][maxn];

void init(int w, int b) {
for (int i = 0; i <= w; i++)
for (int j = 0; j <= b; j++)
if (i == 0)
dp[i][j] = 0;
else if (j == 0)
dp[i][j] = 1;
else
dp[i][j] = -1;
}

double getAns(int w, int b, int deep) {
//for (int i = 0; i < deep; i++)
// printf("\t");
//printf("dp[%d][%d]:\n", w, b);

if (w < 0 || b < 0)
    return 0;

if (dp[w][b] < 0) {
    // get white directly
    dp[w][b] = (double)w / (w + b);

    
    if (w + b >= 3) {
        // white
        dp[w][b] +=
            ((double)b / (w + b)) * ((double)(b - 1) / (w + b - 1)) *
            ((double)w / (w + b - 2)) * getAns(w - 1, b - 2, deep + 1);

        // black
        dp[w][b] +=
            ((double)b / (w + b)) * ((double)(b - 1) / (w + b - 1)) *
            ((double)(b - 2) / (w + b - 2)) * getAns(w, b - 3, deep + 1);
    }
}

//for (int i = 0; i < deep; i++)
//    printf("\t");
//printf("%.9f\n", dp[w][b]);

return dp[w][b];

}

int main() {
int w, b;
while (scanf("%d%d", &w, &b) != EOF) {
init(w, b);
printf("%.10f\n", getAns(w, b, 1));
}
return 0;
}

{% endfold %}