Fear Factoring Gym - 101652P(除法分块)
转换成1到n的约数和
写一下
F(1) = {1};
F(2) = {1, 2};
F(3) = {1, 3};
F(4) = {1, 2, 4};
F(5) = {1, 5};
F(6) = {1, 2, 3, 6};
所以就是在求
(n/1)* 1 + (n/2)2+(n/3)3+(n/4)4+…+(n/n)n
代码里有注释
#include<bits/stdc++.h>
#define int unsigned long long
const int maxn=1e5+10;
const int mod=1e9+7;
int n;
ll cal(int n)
{
int ans=0;
for(int i=1,j;i<=n;i=j+1)//枚举因子
{
j=n/(n/i);
//j是与i出现次数相同的最大因子
ans+=(i+j)*(j-i+1)*(n/i)/2;
//i+j 首项加末项 j-i+1项数 n/i出现的次数
}
return ans;
}
#undef int
int main()
{
#define int unsigned long long
int a,b;
while(cin>>a>>b)
{
cout<<cal(b)-cal(a-1)<<endl;
}
return 0;
}