除法分块。
猜想: 记 (g(x)=lfloor k / lfloor k / x
floor
floor),则对于 (i in [x,g(x)]),(lfloor k / i
floor) 都相等。
证明: 显然函数 (y=k/x) 单调递减。显然 (lfloor k/x
floor leq k/x)。则:
- (g(x)=lfloor k / lfloor k / x floor floor geq lfloor k/(k/x) floor=x Rightarrow lfloor k/g(x) floor leq lfloor k/x floor);
- (lfloor k/g(x) floor=lfloor k/lfloor k / lfloor k / x floor floor floor geq lfloor k/( k / lfloor k / x floor ) floor=lfloor k/x floor)。
于是 (lfloor k/g(x) floor=lfloor k/x floor)。则显然对于 (i in [x,g(x)]),(lfloor k / i floor) 都相等。我们还可以知道 (lfloor k/(g(x)+1) floor < lfloor k/g(x) floor=lfloor k/x floor)。
回到问题,(ans=sum_{i=1}^n k mod i=nk-sum_{i=1}^{min(n,k)} lfloor k/i floor imes i),当 (lfloor k/i floor) 相等时对 (i) 用等差数列求和就好了。
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
int n, k, lst, end;
ll ans=0;
int main(){
cin>>n>>k;
for(int i=1; i<=min(k,n); i=end+1){
lst = k / i;
end = min(n, k / lst);
ans += (ll)lst * (end+i) * (end-i+1) / 2;
}
cout<<(ll)n*k-ans<<endl;
return 0;
}