problems / 252

Alice, Bob and the Leaky Ball Bag

medium probability dp games math 1000 ms · 256 MB

Alice, Bob and the Leaky Ball Bag

Alice and Bob found an old cloth bag with p pink balls and g grey balls inside, and agreed that whoever pulls out a pink ball first wins. There is a catch: the bag has a hole, and Bob draws so clumsily that every time he pulls out a grey ball, the bag jerks and another ball slips out through the hole and rolls away under the sofa.

Formally, the players take turns, Alice first. On each turn:

  1. If the bag is empty, the game ends and Bob wins.
  2. Otherwise the current player draws one ball, chosen uniformly at random among the balls in the bag, and it does not go back.
  3. If the drawn ball is pink, the current player wins and the game ends.
  4. If it is grey and the current player is Bob, then, if the bag is not empty, one more ball chosen uniformly at random among the remaining balls falls out of the bag and is lost. A ball that falls out does not count as anyone's draw, whatever its colour.
  5. The turn passes to the other player.

A grey draw by Alice makes nothing fall out. Find the probability that Alice wins.

Input

A single line with two integers p and g (0 ≤ p, g ≤ 1000): the numbers of pink and grey balls.

Output

Print the probability that Alice wins. Your answer is accepted if its absolute or relative error is at most 10^-9.

Notes

In the first example Alice wins at once with probability 1/4. Otherwise she drew grey and Bob faces one pink and two grey balls: he wins with probability 1/3, and with probability 2/3 he draws grey, after which one of the last two balls falls out. If the pink one falls out, the bag holds only a grey ball and Alice cannot win; if the grey one falls out, Alice draws the pink ball. In total Alice wins with probability 1/4 + 3/4 · 2/3 · 1/2 = 1/2.

If there are no pink balls at all, Alice can never win.

Answers within 1e-09 absolute or relative error are accepted.

Example 1

Input

1 3

Output

0.500000000000

Example 2

Input

2 3

Output

0.600000000000

Hints · reveal one at a time if you're stuck

Hint 1

The only thing that matters at the start of Alice's turn is how many pink and grey balls are left. What is Alice's chance when no pink ball is left, and when no grey ball is left?

Hint 2

Look at one full round: Alice draws, then (if she drew grey) Bob draws, then (if he drew grey too) a ball falls out. The game either ends during the round or comes back to Alice with one pink and two grey fewer, or with three grey fewer.

Hint 3

Let f(p, g) be Alice's winning chance at the start of her turn. Each round only decreases the counts, so you can fill a table of f for all smaller states first and combine them with the probabilities of each branch: a dynamic programming over the state.

report an issue Prefer your own editor? sooks prep 252 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.