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 。

扫描二维码关注公众号,回复: 11603244 查看本文章

输入输出样例

输入 #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;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/108423821