F - Sugoroku2 Editorial by Feecle6418
Let us use dp.
Let \(f_i\) be the expected number of steps from \(i\) to \(n\) (or to any \(m>n\)).
Then:
- \(f_{i}=0\) for \(i\ge n\).
- \(f_{i}=f_{0}\) if \(i\) is one of the numbers in \(A\).
- \(f_{i}=1+\dfrac{1}{m}\sum\limits_{j=i+1}^{i+m}f_j\) otherwise.
The answer is \(f_0\).
When transiting, keep each \(f_i\) in the form of \((k_i,b_i)\), meaning that \(f_i=k_if_0+b_i\). Solving the equation \(f_0=k_0f_0+b_0\) leads to the answer.
With suffix sums, the above can be done in \(O(n+K)\). Long double should be used to avoid float errors.
Example implemention: https://atcoder.jp/contests/abc189/submissions/19630305
posted:
last update: