DOJ logoDOJ
Problems
Recently added problemsSaved problems
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

Easy Divisor Problem

Back to problem

Editorial

Let

D(N)=∑x=1Nτ(x).D(N)=\sum_{x=1}^{N}\tau(x).D(N)=x=1∑N​τ(x).

The value τ(x)\tau(x)τ(x) is the number of ordered pairs of positive integers (a,b)(a,b)(a,b) satisfying ab=xab=xab=x. Therefore

D(N)=#{(a,b)∈Z>02:ab≤N}=∑a=1N⌊Na⌋.D(N)=\#\{(a,b)\in\mathbb Z_{>0}^2:ab\le N\} =\sum_{a=1}^{N}\left\lfloor\frac Na\right\rfloor.D(N)=#{(a,b)∈Z>0

Thus the task is to count positive lattice points under the hyperbola xy=Nxy=Nxy=N.

Subtask 1

For N≤106N\le10^6N≤106, evaluate the sum directly in O(N)O(N)O(N) time.

Subtask 2

The hyperbola is symmetric about x=yx=yx=y. Let X=⌊N⌋X=\lfloor\sqrt N\rfloorX=⌊N​⌋. Then

D(N)=2∑x=1X⌊Nx⌋−X2.D(N)=2\sum_{x=1}^{X}\left\lfloor\frac Nx\right\rfloor-X^2.D(N)=2x=1∑X​⌊

This gives an O(N)O(\sqrt N)O(N​) algorithm, which is fast enough for N≤1012N\le10^{12}N≤10.

Full subtask

For N≤1018N\le10^{18}N≤1018, the square-root method is still too slow. The intended solution uses Richard Sladkey's successive approximation method, which counts the lattice points in O(N1/3)O(N^{1/3})O(N1/3) time and O(log⁡N) extra space.

Set

Xmax⁡=⌊N⌋,Ymin⁡=⌊NXmax⁡⌋X_{\max}=\lfloor\sqrt N\rfloor,\qquad Y_{\min}=\left\lfloor\frac{N}{X_{\max}}\right\rfloorXmax​=⌊N

and in the implementation choose

Xmin⁡=min⁡(Xmax⁡, 2⌈2N3⌉).X_{\min}=\min\left(X_{\max},\ 2\left\lceil\sqrt[3]{2N}\right\rceil\right).Xmin​=min(Xmax​

Columns with x<Xmin⁡x<X_{\min}x<Xmin​ are summed directly, which already costs only O(N1/3)O(N^{1/3})O(N1/3).

For the remaining part, approximate the hyperbola by integral lines. Let two boundary lines be

L1:a1x+b1y=c1,L2:a2x+b2y=c2.L_1:a_1x+b_1y=c_1,\qquad L_2:a_2x+b_2y=c_2.L1​:a1​x+b

Choose their slopes as Farey neighbors, so

a1b2−a2b1=1.a_1b_2-a_2b_1=1.a1​b2​−a2​b

Define transformed coordinates

u=a1x+b1y−c1,v=a2x+b2y−c2.u=a_1x+b_1y-c_1,\qquad v=a_2x+b_2y-c_2.u=a1​x+b1​y

Because the determinant is 111, integer lattice points correspond bijectively. The inverse transform is

x=b2(u+c1)−b1(v+c2),x=b_2(u+c_1)-b_1(v+c_2),x=b2​(u+c1​)− y=a1(v+c2)−a2(u+c1).y=a_1(v+c_2)-a_2(u+c_1).y=a1​(v+c2​)−

Solving the transformed hyperbola for vvv gives

V(u)=(a1b2+b1a2)(u+c1)−(u+c1)2−4a1b1N2a1b1−c2.V(u)=\frac{(a_1b_2+b_1a_2)(u+c_1)-\sqrt{(u+c_1)^2-4a_1b_1N}}{2a_1b_1}-c_2.V(u)=2a

Similarly,

U(v)=(a1b2+b1a2)(v+c2)−(v+c2)2−4a2b2N2a2b2−c1.U(v)=\frac{(a_1b_2+b_1a_2)(v+c_2)-\sqrt{(v+c_2)^2-4a_2b_2N}}{2a_2b_2}-c_1.U(v)=2a

If one side of a region is small, sum ⌊V(u)⌋\lfloor V(u)\rfloor⌊V(u)⌋ or ⌊U(v)⌋\lfloor U(v)\rfloor⌊U(v)⌋ directly.

Otherwise take the mediant

a3=a1+a2,b3=b1+b2.a_3=a_1+a_2,\qquad b_3=b_1+b_2.a3​=a1​+a

The point where the transformed hyperbola has slope −1-1−1 has

utan=(a1b2+b1a2+2a1b1)Na3b3−c1.u_{\mathrm{tan}}=(a_1b_2+b_1a_2+2a_1b_1)\sqrt{\frac{N}{a_3b_3}}-c_1.utan​=(a1​

Set u4=⌊utan⌋u_4=\lfloor u_{\mathrm{tan}}\rflooru4​=⌊utan​⌋, u5=u4+1u_5=u_4+1u, and

v4=⌊V(u4)⌋,v5=⌊V(u5)⌋,v_4=\lfloor V(u_4)\rfloor,\qquad v_5=\lfloor V(u_5)\rfloor,v4​=⌊V(u4​)⌋,v v6=u4+v4,u7=u5+v5.v_6=u_4+v_4,\qquad u_7=u_5+v_5.v6​=u4​+v

With Δ(k)=k(k+1)/2\Delta(k)=k(k+1)/2Δ(k)=k(k+1)/2, the lattice points in the middle polygon are counted in the implementation by

Δ(v6−1)−Δ(v6−u5)+Δ(u7−u5).\Delta(v_6-1)-\Delta(v_6-u_5)+\Delta(u_7-u_5).Δ(v6​−1)−Δ(v6​

The two remaining regions are bounded by (a1/b1,a3/b3)(a_1/b_1,a_3/b_3)(a1​/b1​,a3​/b and , respectively, so the same procedure recurses on both.

At the top level, process the regions corresponding to integral slopes −1,−2,−3,…-1,-2,-3,\ldots−1,−2,−3,…. A slope −a-a−a is tangent around x≈N/ax\approx\sqrt{N/a}x≈N/. Stop once this point falls below and finish the short remainder directly. Polygonal parts between consecutive regions are counted with triangular-number formulas.

Sladkey's analysis bounds the total number of visited regions by O(N1/3)O(N^{1/3})O(N1/3), while the recursion depth is O(log⁡N)O(\log N)O(logN). Hence the total time complexity is O(N1/3)O(N^{1/3})O( and the extra space complexity is .

For N=1018N=10^{18}N=1018, the exact value D(N)D(N)D(N) exceeds signed 64-bit range. Use __int128 for the exact lattice-point count and relevant products, and reduce modulo 998 244 353998\,244\,353998244353 only at the end.

Solution written by GPT5.6

2
​
:
ab≤
N}=
a=1∑N​⌊aN​⌋.
x
N
​
⌋
−
X2.
12
O(\log N)
O(logN)
​
⌋
,
Ymin​
=
⌊Xmax​N​⌋
,
 
2
⌈32N​⌉
)
.
1​
y
=
c1​,L2​:
a2​x+
b2​y=
c2​.
1
​
=
1.
−
c1​,v=
a2​x+
b2​y−
c2​.
b1​
(
v
+
c2​),
a2​
(
u
+
c1​).
1
​
b1​
(a1​b2​+b1​a2​)(u+c1​)−(u+c1​)2−4a1​b1​N
​
−
c2​.
2
​
b2​
(a1​b2​+b1​a2​)(v+c2​)−(v+c2​)2−4a2​b2​N
​
−
c1​.
2
​
,
b3​
=
b1​+
b2​.
b2​
+
b1​a2​+
2a1​b1​)a3​b3​N​​−
c1​.
5​
=
u4​+
1
5​
=
⌊V(u5​)⌋,
4
​
,
u7​
=
u5​+
v5​.
−
u5​)+
Δ(u7​−
u5​).
3
​
)
(a3/b3,a2/b2)(a_3/b_3,a_2/b_2)(a3​/b3​,a2​/b2​)
a
​
Xmin⁡X_{\min}Xmin​
N
1/3
)
O(log⁡N)O(\log N)O(logN)
​
​