Premium problem71. Escaping in Two Stages

Medium Locked

An escape needs two stages done back to back. Stage one succeeds with probability p1, stage two with p2. Failing either stage sends you back to the start. Each stage you attempt counts as one action.

Return the expected total number of actions until you get out.

Condition on one pass through:

E=1+(1−p1)E+p1(1+(1−p2)E)⇒E=1+p1p1p2E = 1 + (1-p_1)E + p_1\bigl(1 + (1-p_2)E\bigr) \quad\Rightarrow\quad E = \frac{1 + p_1}{p_1 p_2}

Input

p1 = 0.5
p2 = 0.5

Output

6.0

Premium problem

This one's part of Premium. Unlock the full Probability track plus every other premium problem on the site.

Implement solve(...)