Took me a while to even convince myself the recursion was right.
Model the process as a Markov chain or dynamic programming over positions, where the probability of winning from position i is the average of the probabilities from i+1 to i+K. Define base cases: probability 1 if i is in [mn, mx], and 0 if i > mx. Then compute the probabilities backwards from mx down to 0, using a sliding window to optimize the recurrence.
Pro tip: Mention that the recurrence can be computed in O(mx) time with a sliding window sum, and note that if K is large relative to the interval, the probability approaches 1/(average step) times interval length, but exact DP is needed for correctness.
Let P(i) be the probability of winning starting from position i. For i in [mn, mx], P(i)=1; for i > mx, P(i)=0. For i < mn, P(i) = (1/K) * sum_{d=1 to K} P(i+d).
Set P(i)=1 for i in [mn, mx] and P(i)=0 for i > mx. Note that positions beyond mx are losing, and positions inside the interval are immediate wins.
Iterate i from mx-1 down to 0, computing P(i) using the recurrence. Use a sliding window sum of the next K probabilities to achieve O(mx) time.
Maintain a running sum of P(i+1) to P(i+K). When moving to i-1, subtract P(i+K) and add P(i) to update the window in O(1) per step.
The answer is P(0). Time complexity is O(mx) and space O(mx) (or O(K) with optimization). Discuss potential numerical issues and edge cases.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.