bzoj1257 数论分块

阅读量: searchstar 2019-11-05 16:47:17
Categories: Tags:


其中1 <= n <= 1e9, 1 <= k <= 1e9

把取模拆开

注意到总共有O(sqrt(k))种取值,可以数论分块。

代码:

#include <iostream>

using namespace std;

typedef long long LL;

LL sum(int l, int r) {
    return (LL)(l + r) * (r - l + 1) / 2;
}

int main() {
    int n, k;

    cin >> n >> k;
    LL ans = 0;
    for (int i = 1, r; i <= k && i <= n; i = r + 1) {
        int d = k / i;
        r = k / d;
        ans += d * sum(i, min(r, n));
    }
    ans = (LL)n * k - ans;
    cout << ans << endl;

    return 0;
}