← Back to Problems
Gambler's Ruin
Easy standard Martingales & Stopping
Time limit: 1 s per test  ·  Memory: 256 MB

A trader starts the night with a bankroll of a chips and plays rounds of a fixed game: each round they win one chip with probability p and lose one chip with probability 1 - p, independently. They stop when the bankroll reaches N chips (target) or 0 chips (bust).

Compute the probability that the trader reaches the target before going bust.

Input

Three values p, a and Np with at most 6 digits after the decimal point, a and N integers.

Output

One real number: the probability of reaching N before 0, with relative error at most 1e-9. Note that the probability can be extremely small; your output must be relatively accurate, not just close to zero.

Constraints

0.05 <= p <= 0.95

1 <= a < N <= 150

Scoring

This is a standard problem: a submission scores full points if it produces the correct output for every test case, and zero otherwise.

Numeric answers are accepted if they are within a relative tolerance of 1e-09 of the expected value.

Examples

input
0.5 3 10
output
3.000000000000000e-01
input
0.45 10 20
output
1.185005313401287e-01
Python 3.13 i Execution environment Isolated microVM · 1 vCPU · no internet access Time and memory limits are set per problem Available packages numpy 2.5.0scipy 1.18.0pandas 3.0.0scikit-learn 1.9.0statsmodels 0.15.0cvxpy 1.9.2