题意
原题链接
求\(1 \sim n\)的约数个数和
sol
直接算很困难,考虑换一个角度
求\(1 \sim n\)的约数个数和,等价于求\(1 \sim n\)分别是范围内几个数的约数
对于第\(i\)个值,在\(1 \sim n\)中,存在\(i, 2 \cdot i,3\cdot i,\cdots,k\cdot i\),共\(\lfloor\frac{n}{i}\rfloor\)
因此,最终的结果为$$\sum_{i=1}^n \lfloor\frac{n}{i}\rfloor$$
代码
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
int main(){
int n;
LL ans = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) ans += n / i;
printf("%lld\n", ans);
return 0;
}
标签:约数,lfloor,lnsyoj98,int,luoguP1403,cdot,include,sim
From: https://www.cnblogs.com/XiaoJuRuoUP/p/18246856/lnsyoj98_luoguP1403