计算每一个数的贡献就好了。。O(N)
n/i只有2*sqrtn个取值于是可以优化到O(sqrtn)
#include#define ll long long using namespace std;const int maxn=500010,inf=1e9;int n,ans,l,r;void read(int &k){ int f=1;k=0;char c=getchar(); while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); while(c<='9'&&c>='0')k=k*10+c-'0',c=getchar(); k*=f;}int main(){ read(n); for(int i=1;i<=n;i=r+1) { int j=n/i;l=n/(j+1)+1;r=n/j; r=min(r,n); ans+=(r-l+1)*j; } printf("%d\n",ans); return 0;}