Conclusion:
there will be at most O(Sqrt(N)) distinct values for floor(N/i).
Prove:
if i < sqrt(N) there will be at most sqrt(N) values
if i > sqrt(N) there will be at most sqrt(N) values (because floor(N/i) < sqrt(N))

sigma K mod i
= sigma K - floor(K / i) * i
= N * K - sigma floor(K / i) * i

we can calculate sigma floor(K / i) * i fastly for all i that floor(K / i) is the same.

Here is the code

for (long long i = 1; i <= min(n, k); i++){
long long t = k / i, r = min(k / t, n);
ans += k * (r - i + 1) - t * (i + r) * (r - i + 1) / 2ll;
i = r;
}
if (n > k) ans += k * (n - k);