题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1257
( sumlimits_{i=1}^{n}k\%i = sumlimits_{i=1}^{n}k-left lfloor k/i ight floor *i )
然后数论分块做即可,注意 ( n>k ) 时右边界的取值。
代码如下:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; int n,k; ll ans; ll cal(int l,int r){return (ll)(l+r)*(r-l+1)/2;} int main() { scanf("%d%d",&n,&k); ans=(ll)n*k; for(int i=1,j;i<=n/*&&i<=k*/;i=j+1) { if(k/i)j=min(n,k/(k/i));//min else j=n; ans-=(ll)(k/i)*cal(i,j); } printf("%lld ",ans); return 0; }