Fear Factoring (除法分块)

在这里插入图片描述
The Slivians are afraid of factoring; it’s just, well, difficult.

Really, they don’t even care about the factors themselves, just how much they sum to.

We can define F(n)F(n) as the sum of all of the factors of nn; so F(6) = 12F(6)=12 and F(12) = 28F(12)=28. Your task is, given two integers aa and bb with a ≤ ba≤b, to calculate S = \sum_{a≤n≤b} F(n)S=∑
a≤n≤b

F(n).

1 Input

The input consists of a single line containing space-separated integers aa and bb (1 ≤ a ≤ b ≤ 10^{12}; b-a ≤ 10^{6} )(1≤a≤b≤10
12
;b−a≤10
6
).

2 Output

Print SS on a single line.

样例输入
101 101
28 28
1 10
987654456799 987654456799
963761198400 963761198400
5260013877 5260489265
样例输出
102
56
87
987654456800
5531765944320
4113430571304040

#include<bits/stdc++.h>
typedef unsigned long long ll;
using namespace std;
ll get_sum(ll n) //除法分块
{
    ll ans=0;
    for(ll l=1,r;l<=n;l=r+1)
	{
        r=n/(n/l);
        ans+=(n/l)*(l+r)*(r-l+1)/2;
    }
    return ans;
}
int main()
{
	ll a,b; //求 a~b中所有数的因子的和
	while(cin>>a>>b)
	{
		cout<<get_sum(b)-get_sum(a-1)<<endl;
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/henulmh/article/details/107867976