题目情境#
题目链接:https://www.luogu.com.cn/problem/P2261 ↗
题目描述
给出正整数 和 ,请计算
其中 表示 除以 的余数。
输入格式
输入只有一行两个整数,分别表示 和 。
输出格式
输出一行一个整数表示答案。
输入 #1
10 5
plaintext输出 #1
29plaintext对于 的数据,保证 。
算法解析:#
整除分块: 的取值只有 段,段 内值相同,其中 。对形如 的求和,可按段累加 的区间和。复杂度 。


c++代码#
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
int main(){
LL n, k, l, r, res;
scanf("%lld%lld", &n, &k);
res = n*k;
for(l=1; l<=n; l=r+1){
if(k/l == 0) break;
r = min(k/(k/l),n);
res -= (k/l)*(r-l+1)*(l+r)/2;
}
printf("%lld", res);
return 0;
}c