[Luogu] 余数求和

question:

$$\sum_{i=1}^{n} k \bmod i$$
$$\sum_{i=1}^{n} k - \lfloor \frac{k}{i} \rfloor i$$
$$\sum_{i=1}^{n}k - \sum_{i=1}^{n}\lfloor \frac{k}{i} \rfloor i$$

直接数论分块

 

#include <iostream>
int main() {
    int n, k; 
    long long Answer = 0;
    std:: cin >> n >> k;
    for(int i = 1, r; i <= n; i = r + 1) {
        if(k / i) r = std:: min(n, (int)k / (k / i));
        else r = n;
        Answer += (1LL * (r - i + 1) * k - 1LL * (k / i) * (i + r) * (r - i + 1) / 2);
    }
    std:: cout << Answer;
    return 0;
}

 

posted @ 2018-06-13 15:03  xayata  阅读(118)  评论(0编辑  收藏  举报