DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord account linking.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Exit K

Back to problem

Editorial

Let EM(i)E_M(i)EM​(i) be the expectation when the current run has length iii and Mog acts next, and define EL(i)E_L(i)EL​(i) similarly. With p=A/(A+B)p=A/(A+B)p=A/(A+B) and q=B/(A+B)q=B/(A+B)q=B/(A+B),

EM(i)=1+pEL(i+1)+qEL(0),E_M(i)=1+pE_L(i+1)+qE_L(0),EM​(i)=1+pEL​ EL(i)=1+qEM(i+1)+pEM(0).E_L(i)=1+qE_M(i+1)+pE_M(0).EL​(i)=1+qEM​

The term with i+1=Ki+1=Ki+1=K is replaced by zero.

Process iii from K−1K-1K−1 down to 000. Every expectation is an affine expression in EM(0)E_M(0)EM​(0) and . At , the two expressions form a linear system. Solving it gives the answer in time and extra space.

(
i
+
1)+
qEL​(0),
(
i
+
1)+
pEM​(0).
EL(0)E_L(0)EL​(0)
i=0i=0i=0
2×22 \times 22×2
O(K)O(K)O(K)
O(1)O(1)O(1)