CT933

Random Walk: Expected Steps to Return

HardAcceptance: 0.0%

In the Random District of Prime City, the Wanderer starts at position 0 on a number line and takes random steps: +1 with probability p or -1 with probability 1-p. The Mathematician wants to know the expected number of steps for the Wanderer to return to position 0. "For a symmetric random walk (p = 0.5), the expected return time is infinite," the Mathematician says. "But for p != 0.5, the expected return time is 1 / (1 - 2*min(p, 1-p)) when the walk is biased. Actually, for a 1D random walk, the probability of ever returning is 1 - |2p - 1|, and the expected number of returns is related. For this problem, we compute the expected number of steps to first return to 0, or -1 if it's infinite." Given p as a fraction a/b (probability of stepping +1), compute the expected number of steps to return to 0. If the expected value is infinite, output -1. Otherwise output as a fraction in lowest terms. Constraints: 0 < a < b <= 1000 Input: 1 2 Output: -1 Input: 1 3 Output: 3 1

Constraints:

0 < a < b <= 1000

Tags:

random-walk probability expected-value math
Loading...
Test Cases:No test cases
No test cases available.
Random Walk: Expected Steps to Return - HARD Coding Problem | CodeTikki