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

Dice and Sum

Back to problem

Editorial

Sort the three values as x≤y≤zx\leq y\leq zx≤y≤z. The sum of differences equals (y−x)+(z−x)+(z−y)=2(z−x)(y-x)+(z-x)+(z-y)=2(z-x)(y−x)+(z−x)+(z−y)=2(z−x). Thus an odd KKK gives zero triples. If K=0K=0K=0, all values must be equal, giving NNN triples.

Otherwise, let d=K/2>0d=K/2>0d=K/2>0. If d≥Nd\geq Nd≥N, the answer is zero. For d<Nd<Nd<N, the minimum xx has choices, and the maximum is . There are strictly intermediate values, each giving six permutations. If the middle value equals either endpoint, each of the two choices gives three permutations. Hence each minimum contributes triples, and the answer is .

Subtask 1: enumerate every ordered triple in O(N3)O(N^3)O(N3) time and O(1)O(1)O(1) extra space. Subtask 2: evaluate the formula in O(1)O(1)O(1) time and O(1)O(1) extra space.

The maximum answer is 6⌊N2/4⌋≤1 500 000 000 000 000 0006\lfloor N^2/4\rfloor\leq 1\,500\,000\,000\,000\,000\,0006⌊N2/4⌋≤1500000000000000000. All required arithmetic fits in signed 64-bit integers. Avoid 32-bit multiplication and floating-point arithmetic.

Solution written by GPT

x
N−dN-dN−d
x+dx+dx+d
d−1d-1d−1
6(d−1)+3+3=6d6(d-1)+3+3=6d6(d−1)+3+3=6d
6d(N−d)6d(N-d)6d(N−d)
O
(
1
)