D. Bag of mice(概率dp)

https://codeforces.com/problemset/problem/148/D


 

题目描述

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 ww white and bb 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 ww and bb ( 0<=w,b<=10000<=w,b<=1000 ).

输出格式

Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10^{-9}10−9 .

题意翻译

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

输入

一行两个数w,bw,b 。

输出

A赢的概率,误差10^{-9}10−9 以内。

数据范围

0\le w,b\le 10000≤w,b≤1000 。

输入输出样例

输入 #1复制

1 3

输出 #1复制

0.500000000

输入 #2复制

5 5

输出 #2复制

0.658730159

 

定义f[i][j]为当前有i只白鼠,j只黑鼠的先手A的获胜概率。

考虑边界:f[i][0]=1.0(先手必赢)  f[i][1]=1.0*i/(i+j) 先手一定要先拿白鼠的概率。

考虑转移:

先手直接拿白 f[i][j]+=1.0*i/(i+j);

先手拿黑,后手拿白  f[i][j]+=0;

先手拿黑,后手拿黑,跑了白。 f[i][j]+=1.0*j/(i+j)*(j-1)/(i+j-1)*(i/(i+j-2)*f[i-1][j-2];

先手拿黑,后手拿黑,跑了黑。f[i][j]+=1.0*j/(i+j)*(j-1)/(i+j-1)*(j-2)/(i+j-2)*f[i][j-3];

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e3+100;
typedef long long LL;
double f[maxn][maxn]; 
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL w,b;cin>>w>>b;
  for(LL i=1;i<=w;i++) f[i][0]=1.0,f[i][1]=1.0*i/(i+1);
  if(b==0||b==1)
  {
  	printf("%.9lf\n",f[w][b]);return 0;
  }
  
  for(LL i=1;i<=w;i++)
  	for(LL j=2;j<=b;j++)
  	{
  		f[i][j]=1.0*i/(i+j);
  		f[i][j]+=1.0*j/(i+j)*(j-1)/(i+j-1)*i/(i+j-2)*f[i-1][j-2];
  		if(j>=3) f[i][j]+=1.0*j/(i+j)*(j-1)/(i+j-1)*(j-2)/(i+j-2)*f[i][j-3];
	}
  printf("%.9lf\n",f[w][b]);	
return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: `kernel.mice(2)` 翻译成中文的意思是 `核心.多重离散数据插补(2)`,是缺失值插补方法之一,用于处理多重离散数据中的缺失值。它是通过模拟马尔科夫链来生成缺失值的多个可能值,然后通过多个回归模型进行插补。其中的参数 2 表示生成的多个可能值的数量。这个方法通常用于处理分类变量中的缺失值。 ### 回答2: 在Python中,kernel.mice(2) 是一种基于多元插补的方法,用于处理数据中的缺失值。该方法使用鼠标算法(Multiple Imputation by Chained Equations, MICE)来估计缺失值,并生成多个完整的数据集。 具体而言,kernel.mice(2)是通过迭代的方式,将数据集中的缺失值替换为估计值。首先,算法通过观察到的数据计算缺失值的条件分布。然后,它使用这些条件分布来模拟缺失值的多个可能值,以创建多个完整的数据集。随后,对于每个数据集,算法会执行建模和分析,得到对应的估计值。最后,每个数据集的估计值被合并成一个单一的结果。 使用kernel.mice(2)可以更好地处理缺失值,因为它考虑了特征之间的相互依赖关系。当数据集中存在多个缺失值或缺失值的分布有一定复杂性时,该方法可以提供更准确的估计。 总之,kernel.mice(2)是一种用于处理缺失值的方法,它通过多元插补和迭代的方式,生成多个完整的数据集,并利用这些数据集进行估计和分析。 ### 回答3: kernel.mice(2)是Python中一个函数的调用,意思是调用名为"kernel.mice"的函数并传入参数值为2。函数名中的"kernel"可能指的是某个核心功能或者框架,而"mice"可能是函数的具体功能或者用途的名称。传入的参数2可能是函数需要使用的某个变量或者数值。具体的功能和参数的含义需要查看函数的定义或者文档来确定。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值