正题
评测记录:https://www.luogu.org/recordnew/lists?uid=52918&pid=P2261
题目大意
求∑i=1nk%i\sum^{n}_{i=1}k\%i∑i=1nk%i。
解题思路
将k%ik\%ik%i展开一下,k−i∗⌊k/i⌋k-i*\lfloor k/i\rfloork−i∗⌊k/i⌋,然后答案就是
∑i=1nk−i∗⌊k/i⌋\sum^{n}_{i=1}k-i*\lfloor k/i\rfloor∑i=1nk−i∗⌊k/i⌋
将k取出来
nk−∑i=1ni∗⌊k/i⌋nk-\sum^{n}_{i=1}i*\lfloor k/i\rfloornk−∑i=1ni∗⌊k/i⌋
然后用等差公式分多块计算∑i=1ni∗⌊k/i⌋\sum^{n}_{i=1}i*\lfloor k/i\rfloor∑i=1ni∗⌊k/i⌋
code
#include<cstdio>
#include<algorithm>
using namespace std;
long long n,ans,k;
int main()
{scanf("%lld%lld",&n,&k);ans=n*k;for(int x=1,gx;x<=n;x=gx+1){gx=k/x?min(k/(k/x),n):n;ans-=(k/x)*(x+gx)*(gx-x+1)/2;}printf("%lld",ans);
}