题目
给定两个数(),求区间中每个元素的约数个数的和
分析
记表示的约数个数,,答案即为
现在问题变为如何快速的求的值
考虑整体:中以为倍数的有个,,现在时间复杂度为还需要优化
又注意到的值的严格非增的:如,序列为.可以考虑将相同的值一起计算
即对于相同的值求一个区间,可知更新时,而(表示序列中大于等于的个数)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
using LL = long long;
LL X, Y;
LL solve(LL);
int main(){
cin >> X >> Y;
cout << solve(Y) - solve(X - 1) << endl;
return 0;
}
LL solve(LL N){
if(N <= 1) return N;
LL i, j, res = 0;
for(i = 1; i <= N; i = j + 1){
j = N / (N / i);
res += (N / i) * (i + j) * (j - i + 1) / 2;
}
return res;
}