Let EM(i) be the expectation when the current run has length i and Mog acts next, and define EL(i) similarly. With p=A/(A+B) and q=B/(A+B),
EM(i)=1+pEL(i+1)+qEL(0),
EL(i)=1+qEM(i+1)+pEM(0).
The term with i+1=K is replaced by zero.
Process i from K−1 down to 0. Every expectation is an affine expression in EM(0) and EL(0). At i=0, the two expressions form a 2×2 linear system. Solving it gives the answer in O(K) time and O(1) extra space.