2020牛客國慶集訓派對day1 E.Zeldain Garden(數論)
題目連結:https://ac.nowcoder.com/acm/contest/7817/E
題目描述
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.
輸入描述:
The input contains one text line with two integers N, M (1 ≤ N ≤ M ≤ 10^12 ), 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.
示例1
輸入
2 5
輸出
9
分析
數論中有個很牛的結論:
n之內的所有因數個數的計算方法為:n / 1 + n / 2 + n / 3 … n / n ;
還有個問題, n 可以到 10^12 ,不能直接線性。
那麼例如當我們列舉 n = 12 時,發現 12 / (7, 8, 9, 10, 11, 12) 都為 1 ,我們可以只計算一次把他們全部計算出來;
或者發現,y = n / x 這個函式關於 y = x 對稱,對稱為點(√n,√n),因此由函式影象面積對稱可以知道,只需求出前 √n 資料範圍內的面積 ans , ans * 2 - √n * √n 即為答案。
程式碼
# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL X(LL n)
{
LL l, r;
LL ans = 0;
for( l = 1; l <= n; l = r+1)
{
r = n/(n/l);
ans += n/l *(r - l + 1);
}
return ans;
/*
for(l=1; l<=t; l++)
ans += n/l;
ans = ans*2-t*t;
return ans;
*/
}
int main(){
int t;
LL x,y;
cin>> x>>y;
printf("%lld\n",X(y)-X(x-1));