#P3251. Evolving to Strong Mode

    ID: 16508 Type: Default 1000ms 256MiB

Evolving to Strong Mode

Evolving to Strong Mode

You are a creature that collects energy rings by eating basic organisms. Each day, if you are not attacked by the evil jellyfish, you obtain an energy ring. However, if you are attacked by a jellyfish (which happens with probability p) and you have at least one energy ring, you must throw away the ring with the smallest energy and you lose the opportunity to obtain a new ring that day. When you have no energy rings, you are safe from the jellyfish and will always obtain an energy ring.

Initially on day 1 you start with zero energy rings. Every energy ring contributes 1 unit of energy. Your total energy is the sum of the energy values of all the rings you currently hold. Once your total energy reaches or exceeds a given threshold T, you evolve into strong mode and can fight back against the jellyfish.

For days when you are not attacked, you gain 1 energy; when attacked (and you have at least one ring) you lose 1 energy because you discard the smallest ring. Notice that when you are at state 0, you always gain 1 energy regardless of the jellyfish probability. For states greater than or equal to 1, the process behaves like a random walk with an upward step occurring with probability 1-p and a downward step with probability p. It can be shown that the expected number of days starting from a state n (where n > 0) satisfies the recurrence:

\[E(n)=1+pE(n-1)+(1-p)E(n+1)\]

with the boundary condition E(T)=0 (evolution occurs immediately once energy reaches T) and the special rule for n=0: E(0)=1+E(1) (since from 0 you always gain 1 energy).

One can show that the closed‐form solution for the expected number of days starting from state 0 is

\[ E(0)=\frac{T}{1-2p}+\left(-\frac{2p(1-p)}{(1-2p)^2}\right)\Bigl(1-\Bigl(\frac{p}{1-p}\Bigr)^T\Bigr) \]

Given the energy threshold T (an integer) and the jellyfish attack probability p (a decimal number with 0 \leq p < 0.5), calculate the expected number of days to evolve. Print the answer rounded to 6 decimal places.

inputFormat

The input consists of a single line containing an integer T (the energy threshold) and a floating‐point number p (the probability of being attacked), separated by a space. It is guaranteed that 0 \leq p < 0.5.

outputFormat

Output the expected number of days required to evolve into strong mode, rounded to 6 decimal places.

sample

1 0.3
1.000000