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 manypopular rock bands. This time Boris has to move a large collection of quality Mexican concertloudspeakers from the port on the North Sea to the far inland capital. As the collection isexpected to be big, Boris has to organize a number of lorries to assure smooth transport. Themultitude 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 leavingeven a single loudspeaker behind. Strict E.U. regulations demand that in the case of largetransport of audio technology, all lorries in the convoy must carry exactly the same number ofpieces of the equipment.
To meet all the regulations, Boris would like to do some planning in advance, despite the fact thathe does not yet know the exact number of loudspeakers, which has a very significant influenceon the choices of the number and the size of the lorries in the convoy. To examine variousscenarios, for each possible collection size, Boris calculates the so-called “variability”, which isthe number of different convoys that may be created for that collection size without violatingthe 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 evenlydivided into 1, 2, 3, or 6 lorries.
Input Specification
The input contains one text line with two integers N, M (1 \leq N \leq M \leq 10^{12})N,M(1≤N≤M≤10
12
), the minimumand the maximum number of loudspeakers in the collection.
Output Specification
Print a single integer, the sum of variabilities of all possible collection sizes betweenNN and MM,inclusive.
样例输入1复制
2 5
样例输出1复制
9
样例输入2复制
12 12
样例输出2复制
6
样例输入3复制
555 666
样例输出3复制
852
题意:
这个题简单读一下,然后稍微找一下规律,就是让你求n到m的每一个数的因子数,然后相加起来,我们做这道题的时候一直T,队友也换了好几种办法,奈何数据太大了,1e12的数据。
思路:
如果平常的那种算法的话,走循环来找因子,必定超时,也是后面补题的时候,看到了一个大佬的办法,这道题得找一下规律
n 因子数 1-n的因子数之和
1 1 1
2 2 3
3 2 5
4 3 8
5 2 10
6 4 14
7 2 16
8 4 20
就会发现1-n的因子数就等于n/i的值之和,i从(1-n),至于为什么,别问,我也不知道,就是看出来的,但1e12还是大,因为y=n/x是一个反比例函数,它是关于(根号x,根号x)对称,所以取对称一半的面积,根据数学公式:总面积=对称一半的面积-根号x*根号x。
就是减去重叠的一个正方形面积。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;//用long long
ll work(ll n)//计算一个数的因子之和
{
ll ans=0;
ll t=sqrt(double(n));//根号n
for(int i=1;i<=t;i++){
ans=ans+n/i;
}
ans=ans*2-t*t;//公式求和
}
int main ()
{
ios::sync_with_stdio(false);
ll n,m;
cin>>n>>m;
cout<<work(m)-work(n-1)<<endl;//从n-m的,跟前缀和差不多原理
return 0;
}