题面:给定n,计算 $ \sum_{i=1}^{n}\frac{n}{i} $
范围:1<=n<=1E12
思路:分块,假设区间[l,r]的结果都相同,即n/l=n/r,根据l可以推算出r,那么这个区间对结果的贡献就是区间长度乘以结果,时间复杂度为O(sqrtn)。
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a; i<=b; i++)
#define per(i,a,b) for(int i=b; i>=a; i--)
int n, ans;
void solve() {
cin >> n;
for (int l = 1, r; l <= n; l = r + 1) {
r = n / (n / l);
ans += (r-l+1) * (n/l);
}
cout << ans << "\n";
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
while (t--) solve();
return 0;
}
标签:分数,求和,long,int,solve,--,define,abc230E
From: https://www.cnblogs.com/chenfy27/p/18062379