版本比较
初中生都能看懂的线性基详解
正文
增加 72 行删除 53 行
12 行未改动13141516171718192021222324396 行未改动全部展开42142242342442542642742842943043143243343443543643743843944044144244344421 行未改动4664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165172 行未改动52052152252346452452546652652752852953061 行未改动59259359459553659659759859960022 行未改动62362462562656762762862963057157263163263363463563663763857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861963964064164222 行未改动66566666766865066967067167267333 行未改动707708709710692693711712713714715716717718719701720721722723724725726708727728729730731232 行未改动全部展开12 行未改动---
$\mathrm{2026.7.8}$:提交全站推荐。
$\mathrm{2026.7.22}$:修复一些代码上的疏漏(第 $k$ 大异或和与线性基求并);修改排版;新增题目 [P14994 异或最短路和](https://www.luogu.com.cn/problem/P14994) 和 [P15044 [UOI 2022 II Stage] 树](https://www.luogu.com.cn/problem/P15044)。$\mathrm{2026.7.22}$:修复一些代码上的疏漏(第 $k$ 大异或和与线性基求并);修改排版;新增题目 [P14994 异或最短路和](https://www.luogu.com.cn/problem/P14994),[P15044 [UOI 2022 II Stage] 树](https://www.luogu.com.cn/problem/P15044),[P17127 [ICPC 2025 Shanghai R] Gemcrate](https://www.luogu.com.cn/problem/P17127)。
$\mathrm{2026.7.26}$:感谢 @[ZHR100102](https://www.luogu.com.cn/user/947153) 对于线性基求交的勘误。
麻烦善良的管理员请通过一下。
---
396 行未改动全部展开::::
---
### [P17127 [ICPC 2025 Shanghai R] Gemcrate](https://www.luogu.com.cn/problem/P17127)
$n$ 个数 $a_i$,进行分组,最大化每组异或和的按位与和。
$n\leq5\times10^5,V\leq2^{60}$。
---
显然至多分两组,否则三组合并成一组结果不变。
记 $\oplus_{i=1}^na_i=s$。分一组结果为 $s$,分两组则结果为 $x\&(x\oplus s)$,考虑将 $a_i$ 与上 $s$ 取反的结果加入线性基求一个最大值。
时间复杂度 $O(n\log V)$。
---
### [P4151 [WC2011] 最大 XOR 和路径](https://www.luogu.com.cn/problem/P4151) / [CF845G Shortest Path Problem?](https://www.luogu.com.cn/problem/CF845G)
给定一张 $n$ 个点 $m$ 条边的带权无向连通图,求 $1$ 号点到 $n$ 号点的(非简单)路径异或和最大 / 小值。
21 行未改动 }}```
---
### [P14994 异或最短路和](https://www.luogu.com.cn/problem/P14994)
给定一张 $n$ 个点 $m$ 条边的带权无向图,求对于每个有序点对之间(非简单)路径异或和最小值之和:
$$\sum_{u=1}^n\sum_{v=1}^n\operatorname{dist}(u,v)$$
若 $u,v$ 无法互达,$\operatorname{dist}(u,v)=0$。
$n,m\leq2\times10^5,V\leq10^{18}$。
---
[题解链接](https://www.luogu.com.cn/article/lydm4eyf)。
若 $u,v$ 可达,则 $\operatorname{dist}(u,v)$ 是 $d_u\oplus d_v$ 在该连通块中所有环构成的线性基中的最小表示。考虑快速计算。
介绍一个性质,假设 $f(x)$ 为 $x$ 在线性基的最小表示, 那么有:
$$f(x\oplus y)=f(x)\oplus f(y)$$
按位考虑,分类讨论易证。
那么原式可以按位计算,时间复杂度 $O((n+m)\log V)$。
```cppfor(int k=0;k<vec.size();++k){ //枚举连通块内的点 int i=vec[k]; f[i]=L.minrep(d[i]); for(int j=0;j<V;++j)if(f[i]&(1ll<<j))++c[j]; //统计每一位1的个数}for(int k=0;k<vec.size();++k){ int i=vec[k]; for(int j=0;j<V;++j){ if(f[i]&(1ll<<j))ans=(ans+(1ll<<j)*(vec.size()-c[j])%mod)%mod; else ans=(ans+(1ll<<j)*c[j]%mod)%mod; }}```
---
### [AT_abc451_g [ABC451G] Minimum XOR Walk](https://www.luogu.com.cn/problem/AT_abc451_g)
给定一张 $n$ 个点 $m$ 条边的带权无向连通图,求点对 $(u,v)$ 的(非简单)路径异或和最小值小于等于 $k$ 的数量。
2 行未改动---
赛时紧张使用 memset 导致 TLE 2 个点,遗憾离场。有幸写过 [题解](https://www.luogu.com.cn/article/20tlfmjk)。
接着上题,设 $d_u$ 在线性基中的最小表示为 $r_u$,现在求 $r$ 中有多少点对 $(u,v)$ 满足 $r_u \oplus r_v \leq k$。接着上题,设 $d_u$ 在线性基中的最小表示为 $f_u$,现在求 $f$ 中有多少点对 $(u,v)$ 满足 $f_u \oplus f_v \leq k$。
我们可以通过将所有 $r_u$ 依次插入一棵二进制 Trie树,并在插入前查询当前树中有多少数与 $r_u$ 的异或值 $\leq k$ 来实现。我们可以通过将所有 $f_u$ 依次插入一棵二进制 Trie树,并在插入前查询当前树中有多少数与 $f_u$ 的异或值 $\leq k$ 来实现。
总之想讲的是处理异或的工具除了线性基,还有 01 Trie,思维不要太死。
时间复杂度 $O((n+m)\log V)$。61 行未改动
vector<pair<int,int>>g[N];struct edge{ int u,v,w; };vector<edge>nt;int d[N],vst[N],r[N];int d[N],vst[N],f[N];queue<int>q;
long long solve(){ n = in(),m = in(),k = in();22 行未改动 } } linearbasis L; for(int i = 0;i < nt.size();++i)L.insert(d[nt[i].u] ^ d[nt[i].v] ^ nt[i].w); for(int i = 1;i <= n;++i)r[i] = L.query(d[i]); for(int i = 1;i <= n;++i)f[i] = L.query(d[i]); T.clear(); long long res = 0; for(int i = 1;i <= n;++i){ res += T.query(r[i],k); T.add(r[i]); res += T.query(f[i],k); T.add(f[i]); } return res;}```::::
---
### [P14994 异或最短路和](https://www.luogu.com.cn/problem/P14994)
给定一张 $n$ 个点 $m$ 条边的带权无向图,求对于每个有序点对之间(非简单)路径异或和最小值之和:
$$\sum_{u=1}^n\sum_{v=1}^n\operatorname{dist}(u,v)$$
若 $u,v$ 无法互达,$\operatorname{dist}(u,v)=0$。
$n,m\leq2\times10^5,V\leq10^{18}$。
---
[题解链接](https://www.luogu.com.cn/article/lydm4eyf)。
若 $u,v$ 可达,则 $\operatorname{dist}(u,v)$ 是 $d_u\oplus d_v$ 在该连通块中所有环构成的线性基中的最小表示。考虑快速计算。
介绍一个性质,假设 $f(x)$ 为 $x$ 在线性基的最小表示, 那么有:
$$f(x\oplus y)=f(x)\oplus f(y)$$
按位考虑,分类讨论易证。
那么原式可以按位计算,时间复杂度 $O((n+m)\log V)$。
```cppfor(int k=0;k<vec.size();++k){ //枚举连通块内的点 int i=vec[k]; f[i]=L.minrep(d[i]); for(int j=0;j<V;++j)if(f[i]&(1ll<<j))++c[j]; //统计每一位1的个数}for(int k=0;k<vec.size();++k){ int i=vec[k]; for(int j=0;j<V;++j){ if(f[i]&(1ll<<j))ans=(ans+(1ll<<j)*(vec.size()-c[j])%mod)%mod; else ans=(ans+(1ll<<j)*c[j]%mod)%mod; }}```
# Part.4 线性基进阶
*本版块介绍线性基的拓展功能,有一定难度。*
22 行未改动参考 yuanquming 大神的 [博客](https://www.cnblogs.com/yuanquming/p/11260668.html)。我并没有很好地理解他的表述(我太菜了),这绝对是本篇文章最难的部分之一,但现归纳成个人认为较容易理解的内容。
假设现在有两个线性空间 $V_1$ 和 $V_2$,线性基分别为 $B_1$ 和 $B_2$,求交集 $V_0=V_1\cap V_2$ 的线性基。
**引理**:记 $S=V_1\cap \operatorname{span}B_2,T=B_2 \setminus S$,若 $B_1\cup T$ 线性无关,则 $S$ 是 $V_0$ 的一组基,即 $\operatorname{span}(S)=V_0$。**引理**:记 $S=V_1\cap B_2,T=B_2 \setminus S$,若 $B_1\cup T$ 线性无关,则 $S$ 是 $V_0$ 的一组基,即 $\operatorname{span}(S)=V_0$。

证明:33 行未改动时间复杂度 $O(\log^2V)$。
```cppLinear_Basis intersect(Linear_Basis B1,Linear_Basis B2){ Linear_Basis B0=B1,_S;int P[logV]; for(int i=0;i<logV;++i)_S.a[i]=0,P[i]=B1.a[i]; Linear_Basis B0=B1,S;int P[logV]; for(int i=0;i<logV;++i)S.a[i]=0,P[i]=B1.a[i]; for(int i=0;i<logV;++i){ ll x=B2.a[i],X=0;if(!x)continue; for(int j=i;j>=0;--j){ if(x&(1ll<<j)){ if(B0.a[j]){ x^=B0.a[j],X^=P[j]; if(x)continue; _S.a[i]=X; S.a[i]=X; } else B0.a[j]=x,P[j]=X; break; } } } return _S; return S;}```
呕心沥血才一知半解(哭)。232 行未改动全部展开