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

Cheongrin's Game Development (Hard)

Back to problem

Editorial

1. Task counts and days

Let TTT be the common final progress and mmm the number of days. Task iii must be selected ci=(T−ai)/bic_i=(T-a_i)/b_ici​=(T−ai​)/bi​ times. The following conditions are necessary and sufficient:

ci∈Z≥0,∑i=1Nci=Km,max⁡ici≤m.c_i\in\mathbb Z_{\ge0},\qquad \sum_{i=1}^N c_i=Km,\qquad \max_i c_i\le m.ci​∈Z≥0​,

Necessity is immediate. For sufficiency, when m>0m>0m>0 days remain, choose the KKK tasks with the largest remaining counts. At least KKK counts are positive, and at most KKK counts equal mmm, so every count equal to mm is selected. Decrementing the selected counts leaves total and maximum at most . Repetition constructs a valid schedule.

2. Bounding the final progress

Set C=1018C=10^{18}C=1018, H=max⁡iaiH=\max_i a_iH=maxi​a, and . Progress cannot decrease, and each task is selected at most once per day, so any solution with satisfies . By the input promise, if no solution with exists in this interval, the answer is impossible. The bound alone does not guarantee , so the day count must also be checked.

3. Generalized CRT

Integrality requires T≡ai(modbi)T\equiv a_i\pmod{b_i}T≡ai​(modbi​) for every task. Maintain the merged congruence T, with , initially . Substituting into the next congruence gives .

Let g=gcd⁡(D,bi)g=\gcd(D,b_i)g=gcd(D,bi​). If ggg does not divide ai−ra_i-ra, there is no solution. Otherwise, with , compute

x≡ai−rg(Dg)−1(modq)x\equiv\frac{a_i-r}{g}\left(\frac Dg\right)^{-1}\pmod qx≡gai​−r​

with 0≤x<q0\le x<q0≤x<q, then update r←r+Dxr\leftarrow r+Dxr←r+Dx and D←DqD\leftarrow DqD←. When , use . Compute the inverse with the extended Euclidean algorithm.

If D>UD>UD>U during merging, the only possible value in [0,U][0,U][0,U] is rrr. Stop merging and directly check T=rT=rT=r against the entire input: congruences, range, total count, maximum count, and day limit. Congruences not yet merged must also be checked.

4. Linear expressions and lower bounds

If all congruences have been merged and D≤UD\le UD≤U, write T=r+DtT=r+DtT=r+Dt. Define integers

αi=r−aibi,βi=Dbi,A=∑iαi,W=∑iβi.\alpha_i=\frac{r-a_i}{b_i},\quad\beta_i=\frac D{b_i},\quad A=\sum_i\alpha_i,\quad W=\sum_i\beta_i.αi​=b

Then ci=αi+βitc_i=\alpha_i+\beta_i tci​=αi​+βi​t and . Since , the day count increases with . Start with .

The condition ci≤mc_i\le mci​≤m becomes (Kβi−W)t≤A−Kαi(K\beta_i-W)t\le A-K\alpha_i(Kβi​−. If , this gives the lower bound

t≥⌈Kαi−AW−Kβi⌉.t\ge\left\lceil\frac{K\alpha_i-A}{W-K\beta_i}\right\rceil.t≥⌈W−Kβi​Kα

Update ℓ\ellℓ with these bounds. Leave the other tasks for the final candidate check. Ceiling division must work correctly for negative numerators as well.

5. The first congruence candidate

The total count must be divisible by KKK, so solve Wt≡−A(modK)Wt\equiv-A\pmod KWt≡−A(modK). Let g=gcd⁡(W,K)g=\gcd(W,K)g=. If does not divide , there is no solution. Otherwise divide by and use a modular inverse to obtain , where and . For , set . The first candidate at least is

t∗=t0+P⌈ℓ−t0P⌉.t_*=t_0+P\left\lceil\frac{\ell-t_0}{P}\right\rceil.t∗​=t0​+P⌈

First check t∗≤⌊(U−r)/D⌋t_*\le\lfloor(U-r)/D\rfloort∗​≤⌊(U−r)/D⌋. If this fails, report impossibility without performing the large multiplication. Otherwise compute T=r+Dt∗T=r+Dt_*T= and directly verify all original conditions.

6. Why one candidate is enough

All conditions with Kβi<WK\beta_i<WKβi​<W were included in the lower bound. For every other task, (Kβi−W)t(K\beta_i-W)t(Kβi​− is nondecreasing, so if its inequality fails at the first candidate, it fails at every later candidate. The day count also increases; if the first candidate has , later candidates cannot help. Thus a successful first candidate is optimal, and a failed first candidate implies impossibility.

7. Integer bounds and complexity

We have U≤1027+109U\le10^{27}+10^9U≤1027+109, and merging only while D≤UD\le UD≤U gives . Reduce residues before multiplying by an inverse. The sums and lower-bound numerators are safely bounded in absolute value by . Multiply the candidate by only after checking its range, then recompute counts from to obtain the day count. Every intermediate value in this implementation therefore fits a signed -bit integer. Do not compute the unrestricted LCM or multiply an out-of-range candidate by .

With B=max⁡ibiB=\max_i b_iB=maxi​bi​ and fixed-width arithmetic, the time complexity is O(Nlog⁡B+log⁡K)O(N\log B+\log K)O(N and the space complexity is .

8. Subtask solutions

In subtask 1, bi=1b_i=1bi​=1. Set di=H−aid_i=H-a_idi​=H−, , and . The conditions become and . If , only an initially equal array is possible. Otherwise find the lower bound and enumerate the residue of from to , giving time.

In subtask 2, enumerate final progress from HHH through 100 000100\,000100000 and check the necessary and sufficient conditions. Since D≤lcm⁡(1,2,3,4,5)=60D\le\operatorname{lcm}(1,2,3,4,5)=60D≤lcm(1,2,3,4,5), , and , the first candidate above has and the minimum feasible progress is below . The enumeration is therefore sufficient and takes time. Subtask 3 uses the full solution.

Solution written by GPT

i=1∑N​
ci​
=
Km,imax​ci​≤
m.
m
K(m−1)K(m-1)K(m−1)
m−1m-1m−1
i
​
U=min⁡i(ai+Cbi)U=\min_i(a_i+Cb_i)U=mini​(ai​+Cbi​)
m≤Cm\le Cm≤C
H≤T≤UH\le T\le UH≤T≤U
m≤Cm\le Cm≤C
T≤UT\le UT≤U
m≤Cm\le Cm≤C
≡r(modD)T\equiv r\pmod D
T≡r(modD)
0≤r<D0\le r<D0≤r<D
r=0,D=1r=0,D=1r=0,D=1
T=r+DxT=r+DxT=r+Dx
Dx≡ai−r(modbi)Dx\equiv a_i-r\pmod{b_i}Dx≡ai​−r(modbi​)
i​
−
r
q=bi/gq=b_i/gq=bi​/g
(gD​)−1
(modq)
Dq
q=1q=1q=1
x=0x=0x=0
i
​
r−ai​
​
,
βi​
=
bi​D​,A=
i∑​αi​,W=
i∑​βi​.
m=(A+Wt)/Km=(A+Wt)/Km=(A+Wt)/K
W>0W>0W>0
ttt
ℓ=max⁡(0,⌈(H−r)/D⌉)\ell=\max(0,\lceil(H-r)/D\rceil)ℓ=max(0,⌈(H−r)/D⌉)
W)t≤
A−
Kαi​
Kβi<WK\beta_i<WKβi​<W
i​
−
A
​
⌉
.
gcd(W,K)
ggg
AAA
ggg
t≡t0(modP)t\equiv t_0\pmod Pt≡t0​(modP)
P=K/gP=K/gP=K/g
0≤t0<P0\le t_0<P0≤t0​<P
P=1P=1P=1
t0=0t_0=0t0​=0
ℓ\ellℓ
Pℓ−t0​​
⌉
.
r
+
Dt∗​
W)t
m>Cm>Cm>C
D′≤1036+1018D'\le10^{36}+10^{18}
D′≤1036+1018
4⋅1032+10154\cdot10^{32}+10^{15}4⋅1032+1015
DDD
TTT
128128128
WWW
log
B
+
logK)
O(N)O(N)O(N)
ai​
S=∑idiS=\sum_i d_iS=∑i​di​
T=H+qT=H+qT=H+q
(N−K)q≥Kmax⁡idi−S(N-K)q\ge K\max_i d_i-S(N−K)q≥Kmaxi​di​−S
S+Nq≡0(modK)S+Nq\equiv0\pmod KS+Nq≡0(modK)
K=NK=NK=N
qqq
000
K−1K-1K−1
O(N+K)O(N+K)O(N+K)
=
60
∣αi∣≤60|\alpha_i|\le60∣αi​∣≤60
N,K≤8N,K\le8N,K≤8
t∗≤967t_*\le967t∗​≤967
58 08058\,08058080
O(100 000N)O(100\,000N)O(100000N)