# Luogu2261 [CQOI2007]余数求和

## Description

$n, k \leq 10^9$

## Code

#include <bits/stdc++.h>

using namespace std;

#define fst first
#define snd second
#define mp make_pair
#define squ(x) ((LL)(x) * (x))
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef pair<int, int> pii;

template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }

int sum = 0, fg = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') fg = -1;
for (; isdigit(c); c = getchar()) sum = (sum << 3) + (sum << 1) + (c ^ 0x30);
return fg * sum;
}

int main() {
#ifdef xunzhen
freopen("math.in", "r", stdin);
freopen("math.out", "w", stdout);
#endif

LL ans = n > m ? (LL)m * (n - m) : 0;
chkmin(n, m);

for (int l = 1, r = 0; l <= n; l = r + 1) {
r = min(m / (m / l), n);
int r1 = m % r, r2 = m % l;
ans += (LL)(r1 + r2) * ((r2 - r1) / (m / l) + 1) / 2;
}
printf("%lld\n", ans);

return 0;
}
posted @ 2019-01-31 10:29 xunzhen 阅读(...) 评论(...) 编辑 收藏