F

题目描述
Boris is the chief executive officer of Rock Anywhere Transport (RAT) company which specializes in supporting music industry. In particular, they provide discount transport for many popular rock bands. This time Boris has to move a large collection of quality Mexican concert loudspeakers from the port on the North Sea to the far inland capital. As the collection is expected to be big, Boris has to organize a number of lorries to assure smooth transport. The multitude of lorries carrying the cargo through the country is called a convoy.
Boris wants to transport the whole collection in one go by a single convoy and without leaving even a single loudspeaker behind. Strict E.U. regulations demand that in the case of large transport of audio technology, all lorries in the convoy must carry exactly the same number of pieces of the equipment.
To meet all the regulations, Boris would like to do some planning in advance, despite the fact that he does not yet know the exact number of loudspeakers, which has a very significant influence on the choices of the number and the size of the lorries in the convoy. To examine various scenarios, for each possible collection size, Boris calculates the so-called “variability”, which is the number of different convoys that may be created for that collection size without violating the regulations. Two convoys are different if they consist of a different number of lorries.
For instance, the variability of the collection of 6 loudspeakers is 4, because they may be evenly divided into 1, 2, 3, or 6 lorries.
输入
The input contains one text line with two integers N , M (1 ≤ N ≤ M ≤ 1012 ), the minimum and the maximum number of loudspeakers in the collection.
输出
Print a single integer, the sum of variabilities of all possible collection sizes between N and M ,inclusive.
样例输入
2 5
**样例输出 **
9

题目大意
给定两个数a,b,求a和b之间所有数的因子个数之和
思路
可以利用图像来求解,首先知道在1~n中,含有因子i的数的个数是n/i
于是想到我们可以将1~n所有的n/i加起来,于是绘制出以下图像:
image
从这里可以看出,y = n / x的图像关于y = x对称,交点为sqrt(n),所以可以将1到sqrt(n)这段图像值乘以2,最后减去绿色的三角形两次,就是这段图像的面积(从1~n对n/i求和)。

代码

#include <iostream>
#include <cmath>

using namespace std;

typedef long long LL;

LL n, m;

LL get(LL x)  // 获得1~x之间所有数的因子个数之和
{
    LL res = 0;
    LL y = sqrt(x);
    for (int i = 1; i <= y; i ++ )
        res += x / i;
    
    res = res * 2 - y * y;
        
    return res;
}

int main()
{
    cin >> n >> m;
    cout << get(m) - get(n - 1) << endl;
    
    return 0;
}
posted on 2021-04-08 17:27  Laurance  阅读(397)  评论(0编辑  收藏  举报