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;
}

 

上一篇:蓝桥杯历届试题(2014) REV-23 数字游戏


下一篇:字符串匹配(hash算法)