正文
增加 562 行删除 4 行
1234579 行未改动8586878889909192939495969798131 行未改动全部展开23023123223323423523623723894 行未改动333334335336# 2026.02.03 扩展欧几里得算法(exGCD)本章范围为整数内。> 定义(1.1)裴蜀方程:求一组整数解 $(x,y)$ 满足:> $$ax+by=\gcd(x,y)$$## 前置知识79 行未改动- [P2613 【模板】有理数取余](https://www.luogu.com.cn/problem/P2613)- [P1516 青蛙的约会](https://www.luogu.com.cn/problem/P1516)- [P5656 【模板】二元一次不定方程 (exgcd)](https://www.luogu.com.cn/problem/P5656)- [P2421 [NOI2002] 荒岛野人](https://www.luogu.com.cn/problem/P2421)# 2026.02.04 乘法逆元## 乘法逆元### 为什么会有乘法逆元除法运算不具有如下性质:$$\frac{a}{b}\bmod m=\left(\frac{a\bmod m}{b\bmod m}\right)\bmod m$$这种局限性导致计算分数取模无法计算。乘法逆元因此出现。131 行未改动全部展开- [P2265 路边的水沟](https://www.luogu.com.cn/problem/P2265)- [P2054 [AHOI2005] 洗牌](https://www.luogu.com.cn/problem/P2054)- [P1762 偶数](https://www.luogu.com.cn/problem/P1762)- [P4881 hby与tkw的基情](https://www.luogu.com.cn/problem/P4881)# 2026.02.05 扩展中国剩余定理(exCRT)## 解决范围给定:$$\begin{cases}94 行未改动将扩展中国剩余定理再次拓展至 $b_ix\equiv a_i\pmod {m_i}$ 的解。- [CF338D GCD Table](https://www.luogu.com.cn/problem/CF338D)较难发现和 exCRT 的关系。- [P4621 [COCI 2012/2013 #6] BAKTERIJE](https://www.luogu.com.cn/problem/P4621)1234579 行未改动8586878889909192939495969798131 行未改动全部展开23023123223323423523623723894 行未改动333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894# 2026.02.03 [1] 扩展欧几里得算法(exGCD)本章范围为整数内。> 定义(1.1)裴蜀方程:求一组整数解 $(x,y)$ 满足:> $$ax+by=\gcd(x,y)$$## 前置知识79 行未改动- [P2613 【模板】有理数取余](https://www.luogu.com.cn/problem/P2613)- [P1516 青蛙的约会](https://www.luogu.com.cn/problem/P1516)- [P5656 【模板】二元一次不定方程 (exgcd)](https://www.luogu.com.cn/problem/P5656)- [P2421 [NOI2002] 荒岛野人](https://www.luogu.com.cn/problem/P2421)# 2026.02.04 [2] 乘法逆元## 乘法逆元### 为什么会有乘法逆元除法运算不具有如下性质:$$\frac{a}{b}\bmod m=\left\lfloor\frac{a\bmod m}{b\bmod m}\right\rfloor\bmod m$$这种局限性导致计算分数取模无法计算。乘法逆元因此出现。131 行未改动全部展开- [P2265 路边的水沟](https://www.luogu.com.cn/problem/P2265)- [P2054 [AHOI2005] 洗牌](https://www.luogu.com.cn/problem/P2054)- [P1762 偶数](https://www.luogu.com.cn/problem/P1762)- [P4881 hby与tkw的基情](https://www.luogu.com.cn/problem/P4881)# 2026.02.05 [3] 扩展中国剩余定理(exCRT)## 解决范围给定:$$\begin{cases}94 行未改动将扩展中国剩余定理再次拓展至 $b_ix\equiv a_i\pmod {m_i}$ 的解。- [CF338D GCD Table](https://www.luogu.com.cn/problem/CF338D)较难发现和 exCRT 的关系。- [P4621 [COCI 2012/2013 #6] BAKTERIJE](https://www.luogu.com.cn/problem/P4621)# 2026.02.06 [4] 欧拉函数## 欧拉函数### 欧拉函数的定义> 定义(4.1)欧拉函数:$\varphi(n)$ 表示 $n$ 以内的正整数与 $n$ 的互质的数的个数。> $$\varphi(n)=\operatorname{size}\{k|1\le k\le n,k\perp n\}$$### 欧拉函数的性质> 性质(4.2):> $$\forall p\in \text{prime},\varphi(p)=p-1$$>> 易证。> 性质(4.3):> $$\forall p\in \text{prime},\varphi(p^k)=p^k-p^{k-1}=p^{k-1}(p-1)$$>> 证明:不互质的数是 $p,2p,\cdots,p^k$,所以互质的数有 $p^k-p^{k-1}$。> 性质(4.4):> $$\varphi(mn)=\varphi(n)\varphi(m),n\perp m$$>> 证明:使用中国剩余定理。> 性质(4.5):> $$\forall a|x,\varphi(ax)=a\varphi(x)$$>> :::info[证明]> 设与 $x$ 互质的数分别为 $d_1,d_2,\cdots d_{\varphi(x)}$。> 易证 $a\perp d_i(1\le i\le \varphi(x))$。> 若 $a>1$,则 $d_1+x,d_2+x,\cdots,d_{\varphi(x)}+x$ 均和 $ax$ 互质。> 同理 $d_i+tx(0\le t<a)$ 小于 $ax$ 且 $(d_i+tx)\perp ax$。> 总计有大于等于 $a\varphi(x)$ 组互质。>> 设与 $x$ 不互质的一个数为 $k$,记 $d=\gcd(n,k),p=\frac{k}{d},q=\frac{n}{d}$,那么 $d+tx=d(p+q)$ 必然和 $ax$ 有公因数 $d$,故上一段构造的集合充分,所以 $\forall a|x,\varphi(ax)=a\varphi(x)$。> :::## 欧拉函数的求法### 求单个欧拉函数的值> 性质(4.6):设 $n=\prod p_i^{e_i}$,则:> $$\varphi(n)=n\prod\frac{p_i-1}{p_i}$$>> :::info[证明]> $$\begin{aligned}\varphi(n)> &=\varphi\left(\prod p_i^{e_i}\right)\\> &=\prod \varphi(p_i^{e_i})\\> &=\prod [p_i^{e_i-1}(p_i-1)]\\> &=\prod \left(p_i^{e_i}\cdot \frac{p_i-1}{p_i}\right)\\> &=n\prod\frac{p_i-1}{p_i}> \end{aligned}$$> :::故可以得到如下代码:```cpp line-numbersint phi(int n){int ans=n;for(int i=2;i*i<=n;i++)if(n%i==0){ans=ans*(i-1)/i;while(n%i==0) n/=i;}if(n>1) ans=ans*(n-1)/n;return ans;}```时间复杂度 $O(\sqrt n)$。:::success[小推论]性质(4.7):$\forall n>2$,$\varphi(n)$ 为偶数。:::info[证明]由于 $\varphi(n)=n\prod\frac{p_i-1}{p_i}$,所以如果 $\varphi(n)$ 含有积质因子,那么 $p_i-1$ 为偶数,不成立。那么 $n$ 只含有质因子 $2$,记 $n=2^k$,则 $\varphi(n)=2^{k-1}$,显然此时只有 $k=1$ 有贡献,即 $n=2$。还有一种情况,$n$ 不含质因子,故 $n=1$。所以性质成立。:::## 推荐练习- [UVA10179 Irreducable Basic Fractions](https://www.luogu.com.cn/problem/UVA10179)- [UVA11327 Enumerating Rational Numbers](https://www.luogu.com.cn/problem/UVA11327)- [P1891 疯狂 LCM](https://www.luogu.com.cn/problem/P1891)这个题有一个很典的 trick:提取 $\gcd$,具体如下:$$\begin{aligned}\sum_{i=1}^n\operatorname{lcm}(i,n)&=\sum_{i=1}^n \frac{i\cdot n}{\gcd(i,n)}\\&=n\sum_{i=1}^n \frac i{\gcd(i,j)}\\&=n\sum_{d|n}\sum_{i=1}^n \frac id[\gcd(i,n)=d]&\color{red}\text{... the trick}\\&=n\sum_{d|n}\sum_{x=1}^{\lfloor n/d\rfloor}x[\gcd(x,n)=1]&\text{ ... }i=xd\\&=n\sum_{d|n}\sum_{x=1}^dx[\gcd(x,n)=1]\end{aligned}$$还有一个 trick,即 $\sum\limits_{i=1}^ni[\gcd(i,n)=1]=\frac{\varphi(n)n}{2}$,原因是 $\gcd(i,n)=\gcd(n-i,n)$,所以每对 $\gcd(i,n)=\gcd(n-i,n)$ 都会贡献 $n$ 的和。一共 $\frac {\varphi(n)}{2}$ 对。- [CF1295D Same GCDs](https://www.luogu.com.cn/problem/CF1295D)需要注意到周期性。- [P3601 签到题](https://www.luogu.com.cn/problem/P3601)# 2026.02.07 [5] 欧拉定理## 欧拉定理### 欧拉定理> 定理(5.1)欧拉定理:若 $a\perp n$,则:> $$a^{\varphi(n)}\equiv 1\pmod n$$### 费马小定理$n$ 为质数时,$\varphi(n)=n-1$,则:$$a^{n-1}\equiv 1\pmod n$$### 欧拉定理的证明设与 $n$ 互质的数为:$$r_1,r_2\cdots r_{\varphi(n)}$$将他们乘 $a$ 模 $n$:$$r_1\cdot a\bmod n,r_2\cdot a\bmod n,\cdots$$上述的性质有:- 与 $n$ 互质;- 没有相等的两个数;- 所有数在 $[1,n)$ 内。于是上面的两个序列所构成的集合相等。后面证乘积相等,以后再写,咕咕咕。## 欧拉定理的应用### 求乘法逆元> 性质(5.2):若 $a\perp n$,则:> $$a^{-1}\equiv a^{\varphi(n)-1}\pmod n$$### 指数降幂> 推论(5.3):若 $a\perp n$,则:> $$a^b\equiv a^{b\bmod \varphi(n)}\bmod n$$## 扩展欧拉定理> 定理(5.4):对于任意正整数 $a,n$ 和非负整数 $b$:> $$a^b\equiv\begin{cases}a^b\bmod n&b<\varphi(n)\\a^{b\bmod \varphi(n)+\varphi(n)}\bmod n&b\ge \varphi(n)\end{cases}$$### 小 trick针对求 $a_1^{a_2^{\cdots^{a_k}}}\bmod m$ 的式子,运用多次扩展欧拉定理,将时间复杂度 $O(k\log V)\to O(\log V\log m)$。## 推荐练习- [P5091 【模板】扩展欧拉定理](https://www.luogu.com.cn/problem/P5091)- [HDU4704 Sum](https://vjudge.net/problem/HDU-4704)- [P4139 上帝与集合的正确用法](https://www.luogu.com.cn/problem/P4139)- [P3601 签到题](https://www.luogu.com.cn/problem/P3601)- [P3747 [六省联考 2017] 相逢是问候](https://www.luogu.com.cn/problem/P3747)# 2026.02.08 [6] 大步小步算法(Baby Step Gaint Step,BSGS)## 适用范围给定素数 $p$,整数 $a,b(0<a<p,0\le b<p)$,求最小非负整数 $x$,使得:$$a^x\equiv b\pmod p$$或判断是否存在。## BSGS 算法设一块长 $m$,$x$ 在 $[0,p-2]$ 范围内可表示为 $x=i\cdot m-j(0\le j<m,i\in [1,\frac pm])$;则 $$a^{i\cdot m}\equiv b\cdot a^j\pmod p$$。进行 BSGS:- Baby Step:所有的 $b\cdot a^j\bmod p$,存入哈希表;- Giant Step:所有的 $a^j\bmod p$,查找是否存在。时间复杂度 $O(m+\frac pm)$,易证 $m=\sqrt p$ 有最优时间复杂度 $O(\sqrt p)$。## 扩展 BSGS 算法### exBSGS 算法出现的原因但是,BSGS 要求 $a\perp p$。而更一般的情况,就需要使用 exBSGS 算法。### exBSGS 算法的过程由 $a^x\equiv b\pmod p$,得到 $a^{x-1}\cdot a+py=b$。使用裴蜀定理判断无解。记 $d=\gcd(a,p)$,则$$\frac ada^{x-1}+\frac pay=\frac bd$$可转化为$$\frac ada^{x-1}\equiv \frac bd\pmod{\frac pd}$$若 $\gcd(a,\frac pd)=1$,可转化为 BSGS:$$a^{x-1}\equiv \left(\frac ad\right)^{-1}\cdot \frac bd\pmod {\frac pd}$$**注意答案需要 +1.**否则,继续迭代。### exBSGS 的局限此算法求出来的值不一定为最小值:设迭代次数为 $tot$,最后一次算出的答案为 $x$,$x+tot$ 一定为合法解,$[0,tot)$ 的值没有验证,需要暴力判断。# 2026.03.08 [7] 矩阵快速幂、线性递推优化## 矩阵> 定义(7.1)矩阵:将 $m\times n$ 个数排成 $m$ 行 $n$ 列的数表,记作 $M_{m\times n}$,第 $i$ 行 $j$ 列的元素记作 $M_{i,j}$。> 定义(7.2)方阵:若 $A_{m\times n}$ 满足 $m=n$,则 $A_{m\times n}$ 为方阵。> 定义(7.3)零矩阵:所有矩阵都为零的矩阵,记为 $O$。即:> $$O=\begin{bmatrix}0&0&\cdots&0\\0&0&\cdots&0\\\vdots&\vdots&\ddots&\vdots\\0&0&\cdots&0\end{bmatrix}$$> 对于任意的矩阵 $A$,$AO=OA=O$。> 定义(7.4)单位矩阵:主对角线为 $1$、其余为 $0$ 的方阵被称作单位方阵。记作 $I$。即:> $$I=\begin{bmatrix}1&0&\cdots&0\\0&1&\cdots&0\\\vdots&\vdots&\ddots&\vdots\\0&0&\cdots&1\end{bmatrix}$$> 对于任意的 $n$ 阶方阵 $A$ 乘上 $n$ 阶单位矩阵,有 $IA=AI=A$。>> 定义一个 $I$:> ```cpp line-numbers> Matrix I()> {> Matrix a;> for(int i=1;i<=n;i++) a.a[i][i]=1;> return a;> }> ```> 运算(7.5)矩阵加和:同型矩阵中,对应数相加。即:> $$\begin{bmatrix}a_{11}&a_{12}&\cdots&a_{1n}\\a_{21}&a_{22}&\cdots&a_{2n}\\\vdots&\vdots&\ddots&\vdots\\a_{m1}&a_{m2}&\cdots&a_{mn}\end{bmatrix}+\begin{bmatrix}b_{11}&b_{12}&\cdots&b_{1n}\\b_{21}&b_{22}&\cdots&b_{2n}\\\vdots&\vdots&\ddots&\vdots\\b_{m1}&b_{m2}&\cdots&b_{mn}\end{bmatrix}=\begin{bmatrix}a_{11}+b_{11}&a_{12}+b_{12}&\cdots&a_{1n}+b_{1n}\\a_{21}+b_{21}&a_{22}+b_{22}&\cdots&a_{2n}+b_{2n}\\\vdots&\vdots&\ddots&\vdots\\a_{m1}+b_{m1}&a_{m2}+b_{m2}&\cdots&a_{mn}+b_{mn}\end{bmatrix}$$> :::info[同型矩阵]{open}> 两个矩阵,行数与列数对应相同,称为同型矩阵。> :::> 运算(7.6)矩阵数乘:矩阵每一个数乘上一个数。即:> $$\lambda\begin{bmatrix}a_{11}&a_{12}&\cdots&a_{1n}\\a_{21}&a_{22}&\cdots&a_{2n}\\\vdots&\vdots&\ddots&\vdots\\a_{m1}&a_{m2}&\cdots&a_{mn}\end{bmatrix}=\begin{bmatrix}\lambda a_{11}&\lambda a_{12}&\cdots&\lambda a_{1n}\\\lambda a_{21}&\lambda a_{22}&\cdots&\lambda a_{2n}\\\vdots&\vdots&\ddots&\vdots\\\lambda _{m1}&\lambda a_{m2}&\cdots&\lambda a_{mn}\end{bmatrix}$$> 运算(7.7)矩阵乘法:大小 $n\times p$ 的矩阵 $A$ 和大小 $p\times m$ 的矩阵 $B$ 相乘为大小为 $n\times m$ 的矩阵 $C$。有:> $$c_{ij}=\sum_{k=1}^{p}a_{ik}b_{kj}$$> 时间复杂度 $O(nmp)$。>> 矩阵乘法也可以看做 $C_{ij}$ 等于 $a_i$ 与 $b_j$ 对应元素乘积之和。>> 代码实现如下:> ```cpp line-numbers>Matrix operator * (Matrix a,Matrix b)>{> Matrix c;> for(int i=1;i<=n;i++)> for(int k=1;k<=n;k++)> for(int j=1;j<=n;j++)> c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j]%mod)%mod;> return c;> }> ```更一般的形式有:```cpp line-numbersstruct Matrix{int n,m;int a[3005][3005];};Matrix operator * (Matrix a,Matrix b){Matrix c;for(int i=1;i<=a.n;i++)for(int k=1;k<=a.m;k++)for(int j=1;j<=b.m;j++)c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j]%mod)%mod;c.n=a.n;c.m=b.m;return c;}```> 性质(7.8)矩阵乘法结合律:$$(A\times B)\times C=A\times (B\times C)$$:::warning{open}矩阵乘法不满足 $A\times B$ 一定等于 $B\times A$,即不满足交换律。:::## 矩阵快速幂矩阵的乘方:$A^k=\underbrace{A·A·A·A·\cdots· A}_{k\texttt{个}A}$由于矩阵有结合律,所以可以使用快速幂优化。求大小为 $k\times k$ 的矩阵的 $n$ 次方的时间复杂度是 $O(k^3\log n)$。```cpp line-numbersMatrix qpow(Matrix a,int b){Matrix ans=I();while(b){if(b&1) ans=ans*a;a=a*a;b>>=1;}return ans;}```## 矩阵快速幂线性递推### 优化 [$f_n=f_{n-1}+f_{n-2}$](https://www.luogu.com.cn/problem/P1962)定义 $S_n=\begin{bmatrix}f_n\\f_{n-1}\end{bmatrix}$,如果可以寻找一个矩阵 $M$ 使得 $S_i=MS_{i-1}$,那么便可以使用矩阵快速幂,算出 $M^{n-k}$,直接令 $S_n=MS_k$ 即可。可以使用待定系数法:设定义的状态中那一列数可以表示为 $s_1,s_2,\cdots s_n$,$M_i$ 的元素 $M_{ij}$ 一定满足 $s_i=\sum\limits_{j=1}^nM_{ij}S_{n-1,ij}$。证明是矩阵乘法的定义。构造完毕。比如这个题,因为:$$\boxed{\begin{gather}f_n=\mathbf1f_{n-1}+\mathbf1f_{n-2}\\f_{n-1}=\mathbf1f_{n-1}+\mathbf0f_{n-2}\end{gather}}$$所以 $M=\begin{bmatrix}1&1\\1&0\end{bmatrix}$。## 推荐练习- [B2105 矩阵乘法](https://www.luogu.com.cn/problem/B2105)- [P3390 【模板】矩阵快速幂](https://www.luogu.com.cn/problem/P3390)- [P1962 斐波那契数列](https://www.luogu.com.cn/problem/P1962)- [P1939 矩阵加速(数列)](https://www.luogu.com.cn/problem/P1939)- [P4838 P哥破解密码](https://www.luogu.com.cn/problem/P4838)- [P4910 帕秋莉的手环](https://www.luogu.com.cn/problem/P4910)- [P2886 [USACO07NOV] Cow Relays G](https://www.luogu.com.cn/problem/P2886)- [P4159 [SCOI2009] 迷路](https://www.luogu.com.cn/problem/P4159)- [CF1117D Magic Gems](https://www.luogu.com.cn/problem/CF1117D)# 2026.03.15 [8] 高斯消元## 前置知识### 线性方程组> 定义(8.1)$n$ 元一次线性方程组:由 $n$ 个未知数,$m$ 个一次方程组组成的方程组,形如:> $$\begin{cases}a_{11}x_1+a_{12}x_2+\cdots+a_{1n}x_n=b_1\\a_{21}x_1+a_{22}x_2+\cdots+a_{2n}x_n=b_2\\\vdots\\a_{m1}x_1+a_{m2}x_2+\cdots+a_{mn}x_n=b_n\end{cases}$$> 其中有:> - 系数:$a_{ij}$ 表示第 $i$ 个方程第 $j$ 个未知数 $x_j$ 的系数。> - 常数项:$b_i$ 表示第 $i$ 个方程右边的常数。> - 解:一组满足所有方程组的值。### 增广矩阵定义(8.2)增广矩阵:将$$\begin{cases}a_{11}x_1+a_{12}x_2+\cdots+a_{1n}x_n=b_1\\a_{21}x_1+a_{22}x_2+\cdots+a_{2n}x_n=b_2\\\vdots\\a_{m1}x_1+a_{m2}x_2+\cdots+a_{mn}x_n=b_n\end{cases}$$的增广矩阵记作;$$R=\begin{bmatrix}a_{11}&a_{12}&\cdots&a_{1n}&|&b_1\\a_{21}&a_{22}&\cdots&a_{2n}&|&b_2\\\vdots&\vdots&\ddots&\vdots&|&\vdots\\a_{n1}&a_{n2}&\cdots&a_{nn}&|&b_n\end{bmatrix}$$### 初等行变换> 性质(8.3)初等行变换:下面三种变换不改变方程的解:> - $R_i\leftrightarrow L_i$:交换两行> - $R_i\times k$:某行乘非 $0$ 常数> - $R_i+k\times R_j$:某行的第 $k$ 倍加到第 $i$ 行### 阶梯型矩阵> 定义(8.4)阶梯型矩阵:形如:> $$R=\begin{bmatrix}a_{11}&a_{12}&\cdots&a_{1n}&|&b_1\\0&a_{22}&\cdots&a_{2n}&|&b_2\\\vdots&\vdots&\ddots&\vdots&|&\vdots\\0&0&\cdots&a_{nn}&|&b_n\end{bmatrix}$$> 性质:阶梯型矩阵可以快速求出原方程的解。### 矩阵的秩> 定义(8.5)矩阵的秩:将一个矩阵转化为阶梯矩阵后,非零行数量称为矩阵的行秩(rank);列秩同理。>> 性质:行秩和列秩相等。### 线性方程组解的分布对于线性方程组,设其增广矩阵为 $m$ 行 $n+1$ 列,设使用初等行变换化简为阶梯矩阵为 $R$。- 情况一:无解。系数矩阵的秩小于增广矩阵的秩。- 情况二:无数解增广矩阵的秩小于 $n$。- 情况三:唯一解系数矩阵的秩等于增广矩阵的秩等于 $n$。## 高斯消元高斯消元:前向消元+回代求解。简单来说就是一个模拟,但并不简单。## 高斯-约旦消元忘掉阶梯型矩阵,定义最简行列梯矩阵表示只有系数 $a_{ii}=1$,其余均为 $0$。则 $x_i$ 解 `a[i][n+1]`。高斯消元无解的优先级大于无限解。```cpp line-numbersconst int N=105;const double eps=1e-7;double a[N][N+1];int solve(int n){for(int i=1;i<=n;i++){int p=0;for(int j=i;j<=n;j++)if(fabs(a[j][i])>fabs(a[p][i]))p=j;for(int j=1;j<=n+1;j++)swap(a[p][j],a[i][j]);double k=a[i][i];if(fabs(k)<eps) continue;for(int j=1;j<=n+1;j++) a[i][j]/=k;for(int j=1;j<=n;j++){if(i==j) continue;double x=a[j][i];for(int k=1;k<=n+1;k++)a[j][k]=a[j][k]-a[i][k]*x;}}for(int i=1;i<=n;i++)if(fabs(a[i][i])<eps&&fabs(a[i][n+1])>eps) return -1;for(int i=1;i<=n;i++)if(fabs(a[i][i])<eps&&fabs(a[i][n+1])<eps) return 1;return 0;}```时间复杂度 $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-numbersint 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$ 随便找,因为如果一个环更优,线性基会把它选出来。