https://vjudge.net/problem/UVA-11426
参考这个博客
https://www.cnblogs.com/war1111/p/7641546.html
修改的地方为:
(n/k-1)*(n/k)/2
#include <bits/stdc++.h> #define inf 2333333333333333 #define N 4000010 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) //by war //2021.1.29 using namespace std; long long n; long long f[N]; long long ans; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } signed main(){ while(cin>>n && n!=0){ ans=0; for(long long k=n;k>=1;k--){ long long sum=0; For(i,2,n){ if(i*k<=n) sum+=f[i*k]; else break; } f[k]=(n/k-1)*(n/k)/2-sum; } for(long long k=n;k>=1;k--) ans+=k*f[k]; o(ans);p(' '); } return 0; }