模型:
(sum_{i=1}^{n}leftlfloorfrac{n}{i}
ight
floor)
假设 (n = 8),那么可得:
(i) | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
---|---|---|---|---|---|---|---|---|
(8/i) | 8 | 4 | 2 | 2 | 1 | 1 | 1 | 1 |
概念:
表中同样的值会连续出现,而相同的值所划分的区间成为一个块。
整除的性质使得从 (1) 到 (n) 的表可根据数值划分为不同的块,且分块数远远小于 (n)。
利用这种性质,我们可以推导出每个分块具体的左右端点位置在哪,这样就可以快速求解出来了。
推导:
假设我们已知某一个分块的左端点 (l),要求解出该分块的右端点 (r)。
设该分块的数值为(k),对于该分块中的每个数(i),有 (k=leftlfloorfrac{n}{i}
ight
floor=leftlfloorfrac{n}{l}
ight]),即 (i imes k le n)。
那么显然满足 (i imes k le n) 成立的最大的 (i) 就是我们要的右端点 (r)。
于是可得:(left{egin{array}{l}k=leftlfloorfrac{n}{l}
ight
floor \ r=max (i), i imes k leq nend{array}
ight.)
推导得:(r=leftlfloorfrac{n}{k}
ight
floor=leftlfloor frac{n}{leftlfloorfrac{n}{l}
ight
floor}
ight
floor)
变形0:
已知 (n,m),求 (sum_{i=1}^{min(n,m)}lfloorfrac{n}{ i}
floorlfloorfrac{m}{i}
floor)
(left{egin{array}{l}k1=leftlfloorfrac{n}{l}
ight
floor,k2= leftlfloorfrac{m}{l}
ight
floor\ r=max (i), i imes k1 leq n,i imes k2 le mend{array}
ight.)
(r = min(lfloorfrac{n}{k1}
floor,lfloorfrac{m}{k2}
floor))
变形1:
已知 (n,a,b),求 (sum_{i=1}^{n}leftlfloorfrac{n}{a i+b}
ight
floor)
令 (i' = a imes i + b),先求出 (leftlfloorfrac{n}{i'}
ight
floor) 的整除分块
可得 (k=leftlfloorfrac{n}{a imes l+b}
ight
floor),(r'=leftlfloorfrac{n}{k}
ight
floor=leftlfloorfrac{n}{leftlfloorfrac{n}{a imes l + b}
ight
floor}
ight
floor)
(left{egin{array}{l}i'=a imes i +b \ r'=max (i')end{array}
ight.)→(egin{cases}a imes i=i'-b\
i=dfrac{i'-b}{a}end{cases})→ (r=max(i)= max(leftlfloorfrac{i'-b}{a}
ight
floor) = lfloorfrac{r'-b}{a}
floor)
$r =left lfloor dfrac{left lfloor dfrac{n}{lfloor dfrac{n}{a imes l+b}
floor }
ight
floor -b}{a}
ight
floor $
变形2:
做法同上。
已知 (n),求 (sum_{i=1}^{n}leftlfloorfrac{n}{i^2}
ight
floor)
令 (i' = i^2),先求出 (leftlfloorfrac{n}{i'}
ight
floor) 的整除分块
可得 (k=leftlfloorfrac{n}{l^2}
ight
floor),(r'=leftlfloorfrac{n}{k}
ight
floor=leftlfloorfrac{n}{leftlfloorfrac{n}{l^2}
ight
floor}
ight
floor)
(left{egin{array}{l}i=sqrt{i'} \ r'=max (i')end{array}
ight.) → (r = max(i) = max(lfloorsqrt{i'}
floor) = lfloor r'
floor = lfloorsqrt{leftlfloorfrac{n}{leftlfloorfrac{n}{l^2}
ight
floor}
ight
floor}
floor)
变形3:
已知 (n),求 (sum_{i=1}^{n}leftlceilfrac{n}{i}
ight
ceil)
当 (n) 整除 (i) 时,(leftlceilfrac{n}{i}
ight
ceil = leftlfloorfrac{n}{i}
ight
floor)
当 (n) 不整除 (i) 时,(leftlceilfrac{n}{i}
ight
ceil = leftlfloorfrac{n}{i}
ight
floor + 1)
于是我们可以用 $lfloor frac{n+i-1}{i}
floor $ 来代替 (lceilfrac{n}{i}
ceil)
原式就转换为了 (sum_{i=1}^{n}lfloorfrac{n+i -1}{i}
floor)
那么有 (left{egin{array}{l}k=leftlfloorfrac{n+l-1}{l}
ight
floor \ r=max (i), i imes k leq n+i-1 end{array}
ight.)
(ecause i imes k le n+i-1 Rightarrow i imes(k-1) le n-1)
( herefore r=leftlfloorfrac{n-1}{k-1}
ight
floor=left[frac{n-1}{leftlfloorfrac{n+l-1}{l} mid-1
ight.}
ight
floor)