题目:余数之和
思路:就是除法分块
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n, k, ans, tot;
int main()
{
scanf("%lld %lld", &n, &k);
tot = n*k;
LL l = 1, r;
ans = 0;
for(l = 1; l <= n; l = r+1)
{
LL t = k / l;
if(t == 0)break;//当k/l==0的时候,分母不能为0,所以跳出
r = k / t;
r = min(r, n);//可能越界,所以需要判断一下
ans += t*(l+r)*(r-l+1)/2;//(l+r)*(r-l+1)/2为等差数列
}
ans = tot - ans;
printf("%lld\n", ans);
return 0;
}
浙公网安备 33010602011771号