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

Input

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

Output

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.

Example

Input

1 3

Output

0.500000000

Input

5 5

Output

0.658730159

题意: 公主和龙玩一个抓老鼠的游戏。袋子里,有两种老鼠,W只白老鼠,b只黑老鼠。一次抓出一只老鼠,公主先抓,龙后抓,龙抓出一只老鼠后,剩下的老鼠中会逃跑掉任意一只(跑掉的这只不算任何人抓的)。先抓到白老鼠的获胜(公主除抓到白老鼠获胜外,其余情况都算输),求公主获胜的概率。

题解:

思考: 对于 w 只白老鼠,b 只黑老鼠,公主要赢的情况
(一) 直接抓到一只白老鼠,概率为 p1 = w/(w+b)
(二) 抓到一只黑老鼠,但是龙也抓住一只黑老鼠,概率为
p2 = (1-p1)*(b-1)/(w+b-1) 然后跑掉一只老鼠,再分两种
跑掉一只白的 p3=w/(w+b-2) 变为 w-1 , b-2 的状态
跑掉一只黑的 p4=(b-2)/(w+b-2) 变为 w , b-3 的状态

dp[i][j] 代表 i 只白老鼠, j 只黑老鼠公主获胜的概率

dp[i][j]=p1 + p2*p3*dp[i-1][j-2] + p2*p3*dp[i][j-3];

 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 #define MAXN 1005
 5 double dp[MAXN][MAXN];
 6
 7 void Init()
 8 {
 9     for (int i=1;i<MAXN;i++)
10     {
11         for (int j=0;j<MAXN;j++)
12         {
13             double p1=0,p2=0;
14             if (i>=1)
15                 p1 = (i*1.0)/(i+j);   //公主赢
16             if (j>=2)
17                 p2 = (1-p1)*(j-1.0)/(i+j-1);   //龙抓黑
18
19             double p3 = 0,p4 = 0;
20             if (i>=1&&j>=2) p3 = (i*1.0)/(i+j-2);
21             if (j>=3) p4 =(j-2.0)/(i+j-2);
22
23             dp[i][j]= p1;
24             if (j>=2) dp[i][j]+=p2*p3*dp[i-1][j-2];
25             if (j>=3) dp[i][j]+=p2*p4*dp[i][j-3];
26         }
27     }
28 }
29
30 int main()
31 {
32     Init();
33     int w,b;
34     scanf("%d%d",&w,&b);
35     printf("%.12lf\n",dp[w][b]);
36     return 0;
37 }

时间: 2024-11-05 20:11:21

Bag of mice(概率DP)的相关文章

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

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

CF 148D. Bag of mice[概率dp]

题目链接:http://codeforces.com/problemset/problem/148/D 题目大意:一袋子里有w个白老鼠,b个黑老鼠:A和B轮流抓老鼠(不放回),谁先抓到白老鼠,谁win:因为B粗鲁,每次抓完一只老鼠,会跑出来一只:A first: 求A win的概率: 题目分析: 此类概率dp的状态比较固定,dp(i , j )表示当前状态Awin的概率: 1:  dp[i][0],A win的概率为 1:dp[0][j] 概率为 0: 2:  dp[i][j]     如下四种

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'

CoderForce 148D-Bag of mice (概率DP求概率)

题目大意:美女与野兽在玩画鸽子的游戏.鸽子在用黑布遮住的笼子里,白色的有w只,黑色的有b只,每次拿出一只作画,谁先画到白色的鸽子谁就赢.美女首先画,因为野兽太丑,它每次画的时候都会吓跑一只鸽子,所有出笼子的鸽子都不在进去.求美女赢得概率.(设定假如没有人画到白色鸽子,算野兽赢). 题目分析:这道题不难,很显然的概率DP.这是我第一次写概率DP,纪念一下... 代码如下: # include<iostream> # include<cstdio> # include<vecto

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

Problem A CodeForces 148D 概率dp

题意:袋子里有w只白鼠和b只黑鼠.龙和公主轮流从袋子里抓老鼠.谁先抓到白色老师谁就赢.公主每次抓一只老鼠,龙每次抓完一只老鼠之后会有一只老鼠跑出来.每次抓老鼠和跑出来的老鼠都是随机的.如果两个人都没有抓到白色老鼠则龙赢.公主先抓.问公主赢的概率. 做了这么多概率dp的题目了,本来接的差不多了,结果一做还是不会...... 下面是看了别人的思路 win[i][j] = i * 1.0 / (i + j); //i只白老鼠j只黑老鼠时公主选白老鼠 win[i][j] += lost[i][j-1]

概率DP入门题

一 概率问题的论文 1.算法合集之<信息学竞赛中概率问题求解初探> 2.有关概率和期望问题的研究 3.算法合集之<浅析竞赛中一类数学期望问题的解决方法> 二 入门题目 1.POJ 3744 Scout YYF I (简单题) 题意:一条路上有n个地雷 ,a[i]代表第i个地雷放的位置,求安全走过这段路的概率 分析:若第k个位置有地雷则安全走过这个位置的方案为在第k-1个位置跳两步概率为(1-p) 从反面考虑 已经安全走过了第i-1个雷 则在第i个雷的死掉的概率为 1-p(从走到a[