跳到正文

算法·理论

整除分块学习笔记

xuchuhan约 7 分钟
原文
文章信息与版本

文章信息

发布
2024年4月3日
收录
2026年3月21日
更新
2026年11月15日
篇幅
约 7 分钟

版本

  1. v1当前

前言

本文是作者学习整除分块后的一些心得,可能有所不足,如有错误,望读者指正。

分块技巧

本文以求解问题:

∑i=1n⌊ni⌋\sum_{i=1}^n\left\lfloor\dfrac{n}{i}\right\rfloor

为例讲述整除分块。

易知当 ⌊ni⌋=d\left\lfloor\dfrac{n}{i}\right\rfloor=d 且 n,dn,d 一定时,ii 的取值范围是一段连续的区间,记作 [L,R][L,R]。

假设 LL 一定,如何快速求出 RR 呢?

令 ⌊nL⌋=d\left\lfloor\dfrac{n}{L}\right\rfloor=d,求最大的 Δ\Delta,此时 R=L+ΔR=L+\Delta 且 ⌊nL⌋=⌊nL+Δ⌋=d\left\lfloor\dfrac{n}{L}\right\rfloor=\left\lfloor\dfrac{n}{L+\Delta}\right\rfloor=d。

将两个式子化为整式可得 Ld+r1=(L+Δ)d+r2=nLd+r_1=(L+\Delta)d+r_2=n,其中 0≤r1,r2<d0\leq r_1,r_2<d。两式相减得 Δd=r1−r2\Delta d=r_1-r_2。

∵Δd=r1−r2\because \Delta d=r_1-r_2

∴r2=r1−Δd\therefore r_2=r_1-\Delta d

又 ∵r2≥0\because r_2\geq0

∴r1≥Δd\therefore r_1\geq\Delta d

∴Δ≤⌊r1d⌋\therefore \Delta\leq\left\lfloor\dfrac{r1}{d}\right\rfloor

又 ∵r1=n mod L=n−L×⌊nL⌋,d=⌊nL⌋\because r_1=n \bmod L=n-L\times\left\lfloor\dfrac{n}{L}\right\rfloor,d=\left\lfloor\dfrac{n}{L}\right\rfloor

∴Δ≤⌊n−L×⌊nL⌋⌊nL⌋⌋\therefore \Delta\leq\left\lfloor\dfrac{n-L\times\left\lfloor\dfrac{n}{L}\right\rfloor}{\left\lfloor\dfrac{n}{L}\right\rfloor}\right\rfloor

∴Δmax⁡=⌊n−L×⌊nL⌋⌊nL⌋⌋=⌊n⌊nL⌋⌋−L\therefore \Delta_{\max}\\=\left\lfloor\dfrac{n-L\times\left\lfloor\dfrac{n}{L}\right\rfloor}{\left\lfloor\dfrac{n}{L}\right\rfloor}\right\rfloor\\=\left\lfloor\dfrac{n}{\left\lfloor\dfrac{n}{L}\right\rfloor}\right\rfloor-L

∴R=L+Δmax⁡=⌊n⌊nL⌋⌋\therefore R=L+\Delta_{\max}=\left\lfloor\dfrac{n}{\left\lfloor\dfrac{n}{L}\right\rfloor}\right\rfloor

然后 LL 的初始值从 11 开始,RR 根据 LL 推导,接着之后的 L=R+1L=R+1。

显然当 L≤nL\leq \sqrt{n} 时 LL 有 不超过n\sqrt{n} 种取值,当 L>nL>\sqrt{n} 时 nL\dfrac{n}{L} 不超过有 n\sqrt{n} 种取值,所以 LL 共有不超过 2n2\sqrt{n} 种取值,加上 L,RL,R 的计算均为 O(1)\mathcal{O(1)} 级别的,所以整个算法的时间复杂度就是 O(n)\mathcal{O(\sqrt{n})},算是较为优秀的了。

例题

P2261 [CQOI2007] 余数求和

简单推导即可:

G(n,k)=∑i=1nk mod i=n×k−∑i=1ni×⌊ki⌋G(n,k)=\sum_{i=1}^n k\bmod i=n\times k-\sum_{i=1}^n i\times\lfloor\dfrac{k}{i}\rfloor。

用等差数列结合整除分块做即可。

code\text{code}:

C++
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
int n,k,L=1,R,ans;
signed main(){
	cin>>n>>k,ans=n*k;
	while(L<=n){
		if(k/L>0)R=min(k/(k/L),n);
		else R=n;//跳右端点
		ans-=(R-L+1)*(L+R)/2*(k/L),L=R+1;//等差数列求和
	}
	cout<<ans;
	return 0;
}

P6583 回首过去

非常有学习意义的一题。

考虑将 xy\dfrac{x}{y} 转化为 b×ca×c\dfrac{b\times c}{a\times c},其中 aa 只包含因数 2,52,5 且 gcd⁡(c,10)=1\gcd(c,10)=1。

我们考虑枚举合法的 cc 并求出同样合法的 a,ba,b 的情况数。

显然 c∈[1,n]c \in [1,n] 且 gcd⁡(10,c)=1\gcd(10,c)=1 时,b∈[1,⌊nc⌋]b\in[1,\lfloor\frac{n}{c}\rfloor],而 aa 则可以是 [1,⌊nc⌋][1,\lfloor\frac{n}{c}\rfloor] 中只含因数 2,52,5 的数,将所有合法的情况数记为 f(⌊nc⌋)f(\lfloor\frac{n}{c}\rfloor)。

故总答案为 f(⌊nc⌋)×⌊nc⌋f(\lfloor\frac{n}{c}\rfloor)\times \lfloor\frac{n}{c}\rfloor 乘上 cc 的取值方案数。

但 n≤1012n\leq10^{12},显然,这个算法还是不够优秀。

等等,⌊nc⌋\left\lfloor\frac{n}{c}\right\rfloor,这个不是整除分块吗?我们显然可以求出 [L,R][L,R] 使得 ⌊nL⌋=⌊nR⌋=k\lfloor\frac{n}{L}\rfloor=\lfloor\frac{n}{R}\rfloor=k,那么时间复杂度不就降至 n\sqrt{n} 级别了吗?

但是还有问题,我们无法直接通过 c%2!=0&&c%5!=0 判断 cc 是否合法了。

那怎么办?通过容斥原理可以解决。

[L,R][L,R] 中合法的 cc 可以这样计算:[L,R][L,R] 中 11 的倍数 −- [L,R][L,R] 中 22 的倍数 −- [L,R][L,R] 中 55 的倍数 ++ [L,R][L,R] 中 1010 的倍数,记为 f′([L,R])f'([L,R])。

但是这样的函数并不是很好求。但是将 f′([L,R])f'([L,R]) 换一种形式,变为 f′([1,R])−f′([1,L])f'([1,R])-f'([1,L]) 就可以通过除法直接求得了。

所以问题就迎刃而解了。

code\text{code}:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,L=1,res;
int g1(int x){return x-x/2-x/5+x/10;}//求c的取值方案数
int g2(int x){//求1~c内a的取值方案数
	int ans=0;
	for(int i=1;i<=x;i<<=1)for(int j=i;j<=x;j*=5)ans++;
	return ans;
}
signed main(){
	cin>>n;
	while(L<=n){
		int R=n/(n/L);
		res+=(g1(R)-g1(L-1))*g2(n/L)*(n/L),L=R+1;
	}
	cout<<res;
	return 0;
}
原文