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

Divisor Chains

Back to problem

Editorial

Call a nonempty subset of S(n)S(n)S(n) satisfying the condition a divisor chain.

For a positive integer mmm, let g(m)g(m)g(m) be the number of divisor chains whose largest element is exactly mmm. Every element of such a chain divides mmm.

If we remove mmm, the remaining chain is either empty or a divisor chain whose largest element is some proper divisor ddd of mmm. Conversely, adding mmm to any such chain yields a chain with largest element mmm. Therefore,

g(m)=1+∑d∣md<mg(d).g(m)=1+\sum_{\substack{d\mid m\\d<m}}g(d).g(m)=1+d∣md<

The initial 111 represents the singleton chain consisting only of mmm.

Each divisor chain selected from S(n)S(n)S(n) has exactly one largest element ddd, and ddd divides nnn. Hence

f(n)=∑d∣ng(d).f(n)=\sum_{d\mid n}g(d).f(n)=d∣n∑​g(d).

Compute g(1),g(2),⋯ ,g(N)g(1),g(2),\cdots,g(N)g(1),g(2),⋯,g(N) in increasing order. Initialize every g(m)g(m)g(m) to 111. Once is known, add it to for every proper multiple of . Since , all contributions from proper divisors are already known when is processed.

The recurrence implies ∑d∣n,d<ng(d)=g(n)−1\sum_{d\mid n,d<n}g(d)=g(n)-1∑d∣n,d<n​g(d)=g(n)−1, so

f(n)=2g(n)−1.f(n)=2g(n)-1.f(n)=2g(n)−1.

Iterating over multiples as in the sieve of Eratosthenes gives time complexity O(Nlog⁡N)O(N\log N)O(NlogN) and space complexity O(N)O(N)O(N). Over 1≤n≤100 0001\le n\le 100\,0001≤n≤100000, the maximum answer is , which fits in signed -bit integers.

Solution written by GPT6

m
​
∑
​
g
(
d
)
.
g(d)g(d)
g(d)
g(m)g(m)g(m)
mmm
ddd
d<md<md<m
g(d)g(d)g(d)
f(86 400)=7 291 135f(86\,400)=7\,291\,135f(86400)=7291135
646464