# P2424 约数和——快速求l~r所有数的约数和的和

## 暴力题解：

#include<cstdio>

int main()
{
int n,ans = 0;
scanf("%d",&n);

for (int i=1; i<=n; ++i)
{
ans += (n/i)*i;
}

printf("%d",ans);

return 0;
}


## 优化

12,6,4,3,2,2,1,1,1,1,1,1

• 1*(7+12)(12-7+1)/2;

## 代码

#include<bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define ll long long
#define ull unsigned long long
#define IOS ios::sync_with_stdio(0),cin.tie(0)
#define gcd(a,b) __gcd(a,b)
#define ft first
#define sd second
#define endl '\n'
#define PI acos(-1.0)
inline void print(__int128 x)
{
if(x<0) {putchar('-'); x=-x;}
if(x>9) print(x/10);
putchar(x%10+'0');
}
const int N = 5e4+9;
ll Sum(ll n) {
if(n<=1) return n;
ll ans=0;
for(ll l=1,r; l<=n; l=r+1)
{
//n/l代表数字[l,r]出现的次数
r=n/(n/l);
ans+=(n/l)*(l+r)*(r-l+1)/2;
}
return ans;
}
int main()
{
IOS;
int x,y;
cin>>x>>y;
cout<<Sum(y)-Sum(x-1)<<endl;
return 0;
}



04-01 44
07-30 49
09-22 19
09-22 34
07-08 46
05-13 413
08-21 162
09-22 14
06-18 7827
10-20 217
11-25 86
04-20 167
10-17 27
03-02 799