The value τ(x) is the number of ordered pairs of positive integers (a,b) satisfying ab=x. Therefore
D(N)=#{(a,b)∈Z>0
Thus the task is to count positive lattice points under the hyperbola xy=N.
Subtask 1
For N≤106, evaluate the sum directly in O(N) time.
Subtask 2
The hyperbola is symmetric about x=y. Let X=⌊N⌋. Then
D(N)=2x=1∑X⌊
This gives an O(N) algorithm, which is fast enough for N≤10.
Full subtask
For 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) time and extra space.
Set
Xmax=⌊N
and in the implementation choose
Xmin=min(Xmax
Columns with x<Xmin are summed directly, which already costs only O(N1/3).
For the remaining part, approximate the hyperbola by integral lines. Let two boundary lines be
L1:a1x+b
Choose their slopes as Farey neighbors, so
a1b2−a2b
Define transformed coordinates
u=a1x+b1y
Because the determinant is 1, integer lattice points correspond bijectively. The inverse transform is
x=b2(u+c1)−y=a1(v+c2)−
Solving the transformed hyperbola for v gives
V(u)=2a
Similarly,
U(v)=2a
If one side of a region is small, sum ⌊V(u)⌋ or ⌊U(v)⌋ directly.
Otherwise take the mediant
a3=a1+a
The point where the transformed hyperbola has slope −1 has
utan=(a1
Set u4=⌊utan⌋, u, and
v4=⌊V(u4)⌋,vv6=u4+v
With Δ(k)=k(k+1)/2, the lattice points in the middle polygon are counted in the implementation by
Δ(v6−1)−Δ(v6
The two remaining regions are bounded by (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,…. A slope −a is tangent around 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), while the recursion depth is O(logN). Hence the total time complexity is O( and the extra space complexity is .
For N=1018, the exact value D(N) exceeds signed 64-bit range. Use __int128 for the exact lattice-point count and relevant products, and reduce modulo 998244353 only at the end.