传送门
题意
给定正整数(n,k),计算((k ; mod; 1)+(k; mod; 2)+(k; mod; 3)+dots +(k; mod; n))的值,
数据范围
(1leq n,k leq 10^{9})
题解
(k; mod ; i = k- lfloor frac{k}{i}
floor imes i),问题转化为计算(n imes k - sum_{i=1}^{n} lfloor frac{k}{i}
floor imes i)
证明(sum_{i=1}^{n}leftlfloorfrac{k}{i}
ight
floor imes i)最多只有(2 sqrt{k})个值,
- 当(i leq sqrt{k})的时候,最多有(sqrt{k})个不同的取值
- 当(i>sqrt{k})的时候,(leftlfloorfrac{k}{i} ight floor < sqrt{k}) , 故最多有(sqrt{k})个不同的取值
易知(leftlfloorfrac{k}{x}
ight
floor)相同的必定是连续的,若某一个相等区间(x)是下界,上界是(leftlfloorfrac{k}{leftlfloorfrac{k}{x}
ight
floor}
ight
floor)
证明:
令 (l = x),(r = leftlfloorfrac{k}{leftlfloorfrac{k}{x}
ight
floor}
ight
floor), (leftlfloorfrac{k}{x}
ight
floor)显然随着(x)增加减少,
[ecauseleftlfloorfrac{k}{x}
ight
floorleq frac{k}{x}, herefore r geqslantleftlfloorfrac{k}{left(frac{k}{x}
ight)}
ight
floor=x
]
[ hereforeleftlfloorfrac{k}{r}
ight
floor leqleftlfloorfrac{k}{x}
ight
floor
]
[egin{array}{l}ecause r leq frac{k}{leftlfloorfrac{k}{x}
floor
ight.} , herefore leftlfloorfrac{k}{r}
ight
floor geqslantleftlfloorfrac{k}{frac{k}{leftlfloorfrac{k}{x}
ight
floor}}
ight
floorend{array}
]
[ herefore leftlfloorfrac{k}{r}
ight
floor geqslant x
]
综上可以得到
[leftlfloor frac{k}{r}
ight
floor=leftlfloorfrac{k}{x}
ight
floor
]
[ecause i inleft[x,leftlfloorfrac{k}{leftlfloorfrac{k}{x}
ight
floor}
ight
floor
ight]区间内的数,lfloorfrac{k}{i}
floor的值都相等
]
时间复杂度:(O(sqrt{k}))
Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll ans;
int main()
{
ll n,k,l,r;
cin>>n>>k;
ans=n*k;
for(l = 1;l<=n;l=r+1)
{
if(k/l == 0) break;//0对答案无贡献
r=min(k/(k/l),n);
ans-=1ll * (k/l) *(l+r)*(r-l+1)/2;//等差数列求和你n*(a1+an)/2
}
cout<<ans<<endl;
}