Fear Factoring(分块)

Problem C — limit 1 second
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) as the sum of all of the factors of n; so F(6) = 12 and F(12) = 28. Your taskis, given two integers a and b with a ≤ b, to calculate

S = X a≤n≤b F(n)
Input
The input consists of a single line containing space-separated integers a and b (1 ≤ a ≤ b ≤ 1e12;b-a ≤ 1e6).

Output
Print S on a single line.

Sample
Input
101 101

Output
102

Input
28 28
Output
56

Input
1 10

Output
87

Input
987654456799 987654456799

Output
987654456800

Input
963761198400 963761198400
Output
5531765944320

Input
5260013877 5260489265

Output
4113430571304040

 

https://cn.vjudge.net/contest/323440#problem/C

求a到b之间的所有的数的因子之和

分块:

当n==12时:

出现次数为12的:1

出现次数为6的:2

出现次数为4的:3

出现次数为3的:4

出现次数为2的:5,6

出现次数为1的:7,8,9,10,11,12

 

#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long

ull f(ull n)
{
    ull ans = 0;
    ull left,right;
    for(left = 1; left <= n; left = right+1)
    {
        right = n/(n/left);//块的右端点
        ans += n/left*(left+right)*(right-left+1)/2;
        //n/left为块中的数的出现次数
        //(left+right)*(right-left+1)/2按等差数列公式求这个块的数的和
    }
    return ans;
}

int main()
{
    ull a,b;
    scanf("%llu %llu",&a, &b);
    printf("%llu\n",f(b)-f(a-1));

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值