NC17450 因数个数和

https://ac.nowcoder.com/acm/problem/17450
数论分块模板题。
对于每一个数 i ,在 x 内都有 x/i 个数的因数含有它。
则最终要求

\[\sum_{i=1}^n\lfloor \frac{n}{i} \rfloor \]

点击查看代码
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    int q;cin >> q;
    while(q--)
    {
        int n;cin >> n;
        int l=1,r=0,ans=0;
        while(l<=n){
            r = n / (n/l);
            ans += (r-l+1) * (n/l);
            l = r + 1;
        }
        cout << ans << endl;
    }

    return 0;
}


posted @ 2022-07-16 13:19  HIVM  阅读(19)  评论(0编辑  收藏  举报