站在当前的i考虑的话 n/i=p; 假设j为最大的满足 k/j >= p 的整数,j <= k/p; 所以 j最大为 floor(k/p);重复这样计算即可;
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; int main() { LL n; int T; scanf("%d",&T); while(T--){ scanf("%lld",&n); LL res=0; for(LL i=1;i<=n;){ LL p = n/i; LL j = n/p; res+=(j-i+1)*p; i=j+1; } printf("%lld\n",res); } return 0; }