其他分享
首页 > 其他分享> > 【UVA】11526 - H(n) 【整除分块】【TB】

【UVA】11526 - H(n) 【整除分块】【TB】

作者:互联网

11526 - H(n)

题意

输入一个整数,求公式的值。

long long H(int n){
    long long res = 0;
    for(int i = 1; i <= n; i = i+1 ){
        res = (res + n/i);
    }
    return res;
}

Sample Input
2
5
10
Sample Output
10
27

题解

整除分块模板,打表可以发现值是一个块状分布的,如果最左边的下标为\(L\),那么右边的下标\(R\)刚好是\(n/(n/L)\)

代码

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    long long T, n;
    cin >> T;
    while (T--) {
        cin >> n;
        long long res = 0;
        for (long long l = 1, r = 0; l <= n; l = r + 1) {
            r = n / (n / l);
            res += (r - l + 1) * (n / r);
        }
        cout << res << endl;
    }

    return 0;
}

标签:10,int,cin,long,Sample,11526,UVA,TB
来源: https://www.cnblogs.com/timus/p/15649474.html