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ββxNβββX2.
This gives an O(Nβ) algorithm, which is fast enough for Nβ€1012.
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 O(logN) extra space.
Set
Xmaxβ=βNββ,Yminβ=βXmaxβNββ
and in the implementation choose
Xminβ=min(Xmaxβ,Β 2β32Nββ).
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
The two remaining regions are bounded by (a1β/b1β,a3β/b3β) and (a3β/b3β,a2β/b2β), 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/aβ. Stop once this point falls below Xminβ 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(N1/3) and the extra space complexity is O(logN).
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.