Soup Servings

You have two soups, A and B, each starting with n mL. On every turn, one of the following four serving operations is chosen at random, each with probability 0.25 independent of all previous turns:

  • Pour 100 mL from type A and 0 mL from type B.
  • Pour 75 mL from type A and 25 mL from type B.
  • Pour 50 mL from type A and 50 mL from type B.
  • Pour 25 mL from type A and 75 mL from type B.

Note:

  • There is no operation that pours 0 mL from A and 100 mL from B.
  • The amounts from A and B are poured simultaneously during the turn.
  • If an operation asks you to pour more than you have left of a soup, pour all that remains of that soup.

The process stops immediately after any turn in which one of the soups is used up.

Return the probability that A is used up before B, plus half the probability that both soups are used up in the same turn. Answers within 10^-5 of the actual answer will be accepted.

Example 1
Inputn = 50
Output0.625
If operations one and two empty A first, operation three empties both at the same time, and operation four empties B first, the result is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.
Example 2
Inputn = 100
Output0.71875
Considering all possible serving operations and subsequent operations, the probability of A emptying first plus half the probability of both emptying together is 0.71875.

Constraints

  • 0 <= n <= 10^9

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate