令 q=⌊n/d⌋,同一个 q 对应的 d 是一段连续区间,因此可以整除分块。时间复杂度为 O(n),空间复杂度为 O(1);计数与乘法使用 long long。
【参考代码】
1/*2Author:Ehundategh3Date:2026/7/214Name:elixir.cpp5You steal,I kill.6*/7#include <cstdio>8#include <algorithm>9using namespace std;10long long n,Ans;11int main(){12 freopen("elixir.in","r",stdin);13 freopen("elixir.out","w",stdout);14 scanf("%lld",&n);15 long long Limit=n/2;16 for(long long Left=1,Right;Left<=Limit;Left=Right+1){17 long long Value=n/Left;18 Right=min(Limit,n/Value);19 Ans+=(Value-1)*(Right-Left+1);20 }21 printf("%lld\n",Ans);22 return 0;23}