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-backed certification.

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

Cats & Soup: Magic Recipe

Back to problem

Editorial

Merge whenever possible

Assign weight 2x−12^{x-1}2x−1 to a level-xxx item. A merge preserves total weight of each type and frees one slot. Repeated merging leaves at most one item at each level below MMM. This normalized representation is uniquely determined by binary carries and uses the fewest slots among representations with the same weight.

Fix an entire sequence of generated types. Whenever another strategy can continue generating along this sequence, the fully normalized strategy can also continue, since it has at least as much free space. As soon as total star weight reaches 2L−12^{L-1}2L−1, merging can create a level-LLL star. Thus immediate merging is no worse for every possible sequence of outcomes. We may normalize after every generation and generate again whenever a slot remains.

DP states

Let S=2L−1S=2^{L-1}S=2L−1 and H=2M−1H=2^{M-1}H=2M−1. Before success, normalized stars are represented by a mask . Moons below level are represented by a mask . Each level- moon permanently occupies a slot.

Let ccc be the number of slots left after excluding those permanent moons, and let F(c,s,t)F(c,s,t)F(c,s,t) be the success probability. The number of occupied remaining slots is popcount⁡(s)+popcount⁡(t)\operatorname{popcount}(s)+\operatorname{popcount}(t)popcount(s)+popcount(t). If it is at least , no new item can be generated and the value is zero. Initialize the layer to zero.

A generated star increments sss, automatically performing all binary carries. Reaching SSS is immediate success. A generated moon increments ttt; reaching HHH creates a permanent moon, resetting ttt to zero and decreasing ccc.

With α=a/b\alpha=a/bα=a/b and β=1−α\beta=1-\alphaβ=1−α,

F(c,s,t)=αA+βB,F(c,s,t)=\alpha A+\beta B,F(c,s,t)=αA+βB, A={1s+1=S,F(c,s+1,t)s+1<S,A=\begin{cases}1&s+1=S,\\F(c,s+1,t)&s+1<S,\end{cases}A={1F(c,s+1,t B={F(c−1,s,0)t+1=H,F(c,s,t+1)t+1<H.B=\begin{cases}F(c-1,s,0)&t+1=H,\\F(c,s,t+1)&t+1<H.\end{cases}B={F(c−1,s

The answer is F(N,0,0)F(N,0,0)F(N,0,0). A transition that creates the target succeeds even if it uses the last free slot. A maximum-level moon cannot be discarded and must reduce ccc.

Process ccc in increasing order and s,ts,ts,t in decreasing order. Only the current and previous capacity layers are needed. Time is O(N2L+M−2)O(N2^{L+M-2})O(N2L+M−2) and space is O(, with at most state evaluations under the full limits.

Subtasks

For subtask 1, memoize a vector containing the actual number of items of every type and level. Perform an available merge, or branch into the two generated types when no merge remains and space is available. There are at most (N+2M2M)\binom{N+2M}{2M}(2MN+2M​) count vectors, at most 848484 in this subtask. This is the supplied naive implementation.

For subtask 2, L=1L=1L=1, so the first generated star wins. If every generated item is a moon, the inventory first gets stuck after

D={2N−1N<M,(N−M+2)2M−1−1N≥MD=\begin{cases}2^N-1&N<M,\\(N-M+2)2^{M-1}-1&N\geq M\end{cases}D={2N−1

generations. The answer is 1−βD1-\beta^D1−βD, computed in O(log⁡998 244 353+log⁡D)O(\log998\,244\,353+\log D)O(log998244353+logD) time including the inverse.

Modular arithmetic

Let P=998 244 353P=998\,244\,353P=998244353. Compute α=abP−2 mod P\alpha=a b^{P-2}\bmod Pα=abP−2mod and . Every generation increases total weight by one, and inventory weight is at most , so only finitely many generations are possible. The reduced denominator divides a power of and is invertible because .

Each sum of two probability products is below 2(P−1)2<2⋅10182(P-1)^2<2\cdot10^{18}2(P−1)2<2⋅1018, so signed 64-bit arithmetic suffices. The exact reduced fraction need not be constructed.

The independent forward reference propagates probability mass by total star and moon weight. For small cases, verification also compared every merge and generation action using exact rational optimal-policy search.

Solution written by GPT

0≤s<S0\leq s<S
0≤s<S
MMM
0≤t<H0\leq t<H0≤t<H
MMM
ccc
c=0c=0c=0
)
​
s+1=S,s+1<S,​
,
0
)
F(c,s,t+1)
​
t+1=H,t+1<H.​
2L+M−2)O(2^{L+M-2})
O(2L+M−2)
5 242 8805\,242\,8805242880
(N−M+2)2M−1−1
​
N<M,N≥M​
P
β=(1−α+P) mod P\beta=(1-\alpha+P)\bmod Pβ=(1−α+P)modP
N2M−1N2^{M-1}N2M−1
bbb
1≤b<P1\leq b<P1≤b<P