余数之和

AcWing-199-余数之和open in new window

分析

见《进阶指南》第141页。

代码

#include <iostream>
using namespace std;

typedef long long LL;

int main () {
    LL n, k;
    cin >> n >> k;
    
    LL res = n * k;
    for (LL l = 1, r; l <= n; l = r + 1) {
        if (k / l == 0) {
            r = n;
        } else {
            r = min(k / (k / l), n);
        }
        
        res -= (r - l + 1) * (k / l) * (l + r) / 2;
    }
    cout << res << endl;
    return 0;
}
最后修改于: