You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 lines
943 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
// 数论分块模板题,是很多题的基础,需要背诵
// j(n,k)=k%1+k%2+k%3+…+k%n
int n, k, l, r;
int ans;
signed main() {
cin >> n >> k;
ans = n * k; // 看题解的推导公式
for (l = 1; l <= n; l = r + 1) { // 枚举左端点,每次跳着走下次的位置就是本次r的位置+1
if (k / l == 0) break; // 1、当k/l=0的时候显然这段以及后面有单调性已经没有贡献了可以 break。
r = min(k / (k / l), n); // 2、注意右端点和n取个min因为>n没有贡献了。
ans -= (k / l) * (l + r) * (r - l + 1) / 2; // 等差数列求和左到右边界内是公差为1的等差数列首项+末项 乘以 项数 除以2
}
cout << ans << endl;
}