版本比较
来追梦 数论
正文
增加 170 行删除 1 行
112345709 行未改动全部展开715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894# 2026.02.03 [1] 扩展欧几里得算法(exGCD)# 2026.02.03 [1] 扩展欧几里得算法(exGCD)本章范围为整数内。> 定义(1.1)裴蜀方程:求一组整数解 $(x,y)$ 满足:> $$ax+by=\gcd(x,y)$$## 前置知识709 行未改动全部展开}```
时间复杂度 $O(n^3)$。## 异或方程组可以使用 bitset 进行优化。```cpp line-numbersint solve(int n){ for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) if(a[j][i]) { swap(a[i],a[j]); break; } if(!a[i][i]) ans[i]=1; for(int j=i+1;j<=n;j++) { if(a[j][i]==1) a[j]^=a[i]; } } for(int i=n;i>=1;i--) for(int j=i+1;j<=n;j++) ans[i]^=(ans[j]*a[i][j]); return 0;}```## 推荐练习- [P3389 【模板】高斯消元法](https://www.luogu.com.cn/problem/P3389)- [P2455 [SDOI2006] 线性方程组](https://www.luogu.com.cn/problem/P2455)- [P5027 Barracuda](https://www.luogu.com.cn/problem/P5027)- [P4035 [JSOI2008] 球形空间产生器](https://www.luogu.com.cn/problem/P4035)- [P3164 [CQOI2014] 和谐矩阵](https://www.luogu.com.cn/problem/P3164)- [P2962 [USACO09NOV] Lights G](https://www.luogu.com.cn/problem/P2962)# 2026.03.22 [9] 线性基## 前置知识> 性质(**9.1**)异或的基本性质:> - $a\oplus b=b\oplus a$> - $a\oplus b\oplus c=a\oplus(b\oplus c)$> - $a\oplus a=0$> - $0\oplus a=a$> - $a\oplus b=a\oplus c\Leftrightarrow b=c$## 线性相关与线性无关> 定义(9.2)线性相关与线性无关:对于> $$\bigoplus\limits_{i=1}^n (c_ia_i)=0$$> 如果 $\forall i,c_i=0$ 是它的唯一解,那么称 $a_i$ 线性无关;否则称其线性相关。## 线性基### 应用场景给定若干个整数,求一个子集:- 异或和最大;- 异或和最小;- 异或和等于 $x$;- 异或和第 $k$ 小。### 线性基的定义线性基满足如下性质:- 完备性;- 线性无关;- 极小性。### 线性基的构造定义 $p_i$ 表示第 $i$ 位的基最高位为 $i$。
对于插入一个 $x$,如果 $x$ 的最高位为 $k$:- 如果 $p_k=0$,那么 $p_k=0$;- 否则,$p_k\ne 0$,那么 $x\gets x\oplus p_k$。
插入:```cpp line-numbersvoid insert(int x){ for(int i=M;i>=0;i--) if(x>>i) { if(p[i]==0) { p[i]=x; return ; } else x^=p[i]; }}```
构造标准线性基还有:- 对于所有的 $p_i$,如果对于其他 $j\ne i$,$p_j$ 的第 $i$ 位等于 $0$。
构造:```cpp line-numbersint build(){ int n=0; for(int i=0;i<=M;i++) for(int j=i+1;j<=M;j++) if(p[j]&(1ll<<i)) p[j]^=p[i]; for(int i=0;i<=M;i++) if(p[i]) p[n++]=p[i]; return n;}```
时间复杂度 $O(n\log V)$。
高斯消元也可以求线性基:```cpp line-numbersint build(int a[],int n){ int len=1; for(int c=M;c>=0&&len<=n;c--) { for(int i=len;i<=n;i++) if(a[i]&(1ll<<c)) { swap(a[i],a[len]); break; } if(!(a[len]&(1ll<<c))) continue; for(int i=1;i<=n;i++) if(i!=len&&(a[i]&(1ll<<c))) a[i]^=a[len]; len++; } return len-1;}```### 线性基的应用- 求子集异或和最大值 **化成标准线性基后**,所有数的异或值。- 求子集异或和最小值 **化成标准线性基后**,第一个非零数。- 判断 $x$ 是否能被表示 尝试插入 $x$,如果可以插入,那么不存在;否则存在。- 求异或的 $k$ 小值 **化成标准线性基后**,将 $k$ 转为二进制,异或对应位的线性基。- 求 $x$ 在线性基中的排名 1. 设已经得到标准化的线性基($p_0\sim p_{cnt-1}$)。 2. 线性基能够得到的不相同的元素共 $2^{cnt}$ 个。 3. 从 $p_0\sim p_{cnt}$ 遍历,如果 $x\ge p_i$ 且 $x\oplus p_i<x$,$x\gets x\oplus p_i$,$k\gets k\operatorname{or}2^i$。 4. 答案是 $k+1$。 ```cpp line-numbers int rk(int x,int len) { int ans=0; for(int i=0;i<len;i++) if(x>=p[i]&&(x^p[i])<x) { x^=p[i]; ans|=(1ll<<i); } return ans+1; } ```- 求 $x$ 在 $a_i$ 所有子集异或和排序后的排名(不同于上一个,这个要求不去重) 只需要用到一个结论即可。 :::info[结论]{open} 所有元素重复的次数相同。 :::## 推荐练习- [P3812 【模板】线性基](https://www.luogu.com.cn/problem/P3812)- [P4570 [BJWC2011] 元素](https://www.luogu.com.cn/problem/P4570)- [P3265 [JLOI2015] 装备购买](https://www.luogu.com.cn/problem/P3265) 1. 所购买的装备是线性无关的 2. 把 $m$ 个属性看做 $m$ 个未知数 3. 依次枚举每个未知数,找到非零的且价格最便宜的武器作为主元。 4. 消元。 5. 主元对应的武器的价格之和即为答案。 6. 时间复杂度变为三次方。- [P4869 albus就是要第一个出场](https://www.luogu.com.cn/problem/P4869)- [P3292 [SCOI2016] 幸运数字](https://www.luogu.com.cn/problem/P3292)- [CF1100F Ivan and Burgers](https://www.luogu.com.cn/problem/CF1100F) 给定 $n$ 个元素,每次询问区间 $[l,r]$ 的线性基。 考虑构造前缀线性基 $pre_i$,不妨对于每一个 $p_j$ 顺便记录 $pos_j$ 表示可以贡献 $p_j$ 的最靠右的位置。 查询时,只需要对于每一个 $pos_j$ 是否在区间中即可。 trick:线性基不一定需要标准化。- [CF845G Shortest Path Problem?](https://www.luogu.com.cn/problem/CF845G) 1. 以点 $1$ 为起点,维护 $dis_i$ 表示 $1\to i$ 的路径异或和。 2. $1\to n$ 的路径中如果有存在 $u$ 出发的环则可以计算贡献; 3. $dis_n$ 随便找,因为如果一个环更优,线性基会把它选出来。