题目背景
Smart最近沉迷于对约数的研究中。
题目描述
对于一个数X,函数f(X)表示X所有约数的和。例如:f(6)=1+2+3+6=12。对于一个X,Smart可以很快的算出f(X)。现在的问题是,给定两个正整数X,Y(X<Y),Smart希望尽快地算出f(X)+f(X+1)+……+f(Y)的值,你能帮助Smart算出这个值吗?
输入输出格式
输入格式:
输入文件仅一行,两个正整数X和Y(X<Y),表示需要计算f(X)+f(X+1)+……+f(Y)。
输出格式:
输出只有一行,为f(X)+f(X+1)+……+f(Y)的值。
和这道题类似,记得除二要在括号外面。
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define ll long long
#define ld long double
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lowbit(x) (x&(-x))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define lcm(a,b) ((a)*(b)/(__gcd((a),(b))))
using namespace std;
const int N = 1e3 + 10;
const int mod = 1e9 + 7;
ll ans;
int main() {
ll x, y;
cin >> x >> y;
for(ll j, i = 1; i <= y; i = j + 1) {
j = y / (y / i);
ans += (i + j) * (y / i) * (j - i + 1) / 2;
}
for(ll j, i = 1; i <= x - 1; i = j + 1) {
j = (x - 1) / ((x - 1) / i);
ans -= (i + j) * ((x - 1) / i) * (j - i + 1) / 2;
}
cout << ans;
return 0;
}