版本比较
勰码金牌学院 S1 课程 16&17. 高斯消元
正文
增加 131 行删除 178 行
89 行未改动90919293949594959697979899100101109 行未改动全部展开21121221321421521521621721821922022 行未改动24324424524624624724824925025116 行未改动2682692702712712722722732742742752762752762772782792802812822792802812822832852862842882892902912852862872882892902912962922932982992942952962972982992 行未改动302303304305310306307308309310315316317318319320321322323324325326327328329330331332333334335336311312313314315342316317318319320321322323324352325326327328116 行未改动全部展开445446447448477478479480481482483484485486487488489490491492493494495496449450451452131 行未改动全部展开58458558658763658858959059159210 行未改动603604605606655656657658659660661662663664665666667668669670671672673674607608609610228 行未改动全部展开839840841842911912913914915916917918919920921922923924925926927928929930931932843844845846339 行未改动全部展开118611871188118912801281128212831284128512861190119111921193119411951196129411971198119912001201158 行未改动全部展开13601361136213631461136413651366146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921367136813691370194 行未改动全部展开15651566156715681695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172315691570157115729 行未改动158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169389 行未改动
在化简为行阶梯形后,我们从底向上观察:- **无解**:若存在某一行,其左边全为 $0$ 但右边不为 $0$,无解。- **唯一解**:不满足上述条件,且 $r$ 等于变量个数 $n$。- **无穷解**:不满足上述条件,且 $r$ 小于变量个数 $n$(即存在自由变量)。- **唯一解**:不满足无解,且 $r$ 等于变量个数 $n$。- **无穷解**:不满足无解,且 $r$ 小于变量个数 $n$(即存在自由变量)。
笼统的思路已经讲完了,下面进入例题讲解代码。笼统的思路已经讲完了,在例题讲代码。
### $n$ 元 $1$ 次异或方程组
形如109 行未改动全部展开
对于一般情况,其对应的模 $m$ 增广矩阵为:
$$A=A=\left [
\begin{array}{c|c}\begin{matrix}22 行未改动
1. 常规的找主元。3. 枚举接下来的每一行 $k \in [r+1, n]$,通过 `while()` 将 $a_{k,i}$ 消为 $0$:* 计算倍数,令 $t = \lfloor a_{k, i} / a_{r, i} \rfloor$。* 自 $j = i$ 开始,将第 $k$ 行元素减去第 $r$ 行的 $t$ 倍。由于涉及减法,需做防负数取模处理:* 自 $j = i$ 开始,将第 $k$ 行元素减去第 $r$ 行的 $t$ 倍。由于涉及减法,需做防负数取模:
$$a_{k,j} \gets ((a_{k,j} - t \times a_{r,j} \pmod m) + m) \pmod m$$
* 检查 $a_{k,i}$ 是否已经成功消为 $0$。若已经为 $0$,则退出 `while()` ,继续消下一行;若不为 $0$,则**直接交换第 $k$ 行与第 $r$ 行**,利用原 $a_{k,i}$ 作为新的除数继续循环相除。16 行未改动* **无解**:若存在任意一行 $k \in [r+1, n]$,左侧为 $0$,右侧 $a_{k, n+1} \not\equiv 0 \pmod m$,则整个方程组**无解**。
##### 2. 回代与第二步判定(自底向上)
第一步判定后,我们从 $i = n$ 到 $i = 1$ 回代,求特解或进行第二步判定。*(令 `pos[i]` 第 $i$ 列主元所在行。若该列无主元,则 `pos[i] = 0`)*第一步判定后,我们进行回代求特解或进行第二步判定。*(令 `p[k]` 为第 $k$ 行的主元所在列号,用 `has_p[i]` 记录第 $i$ 列是否存在主元)*
* **情况 A:`pos[i] == 0`(自由变量)**在模 $m$ 意义下有 $m$ 种取值方案。若构造特解直接令:
* **自由变量(`has_p[i] == 0`)**遍历 $i \in [1, n]$,若该列不存在主元,则 $x_i$ 为自由变量,在模 $m$ 意义下有 $m$ 种取值方案。构造特解直接令:$$x_i = 0$$
* **情况 B:`pos[i] != 0`(非自由变量,设 $k = \text{pos}[i]$)**将已求出的高位未知数 $x_j \, (j > i)$ 代入第 $k$ 行方程,移项并取模,将方程压缩为关于 $x_i$ 的一元线性同余方程:
* **非自由变量(自底向上遍历行,从 $k = r$ 到 $k = 1$)**对于第 $k$ 行,令主元所在列为 $i = p[k]$。将已求出的 $x_j \, (j > i)$ 代入第 $k$ 行方程,移项、取模,将方程变为为关于 $x_i$ 的一元线性同余方程:$$a_{k,i}x_i \equiv b'_i \pmod m$$
其中 $b'_i = \left( \left( a_{k,n+1} - \sum\limits_{j=i+1}^n a_{k,j}x_j \pmod m \right) \pmod m + m \right) \pmod m$。令 $g = \gcd(a_{k,i}, m)$,利用 **exGCD** 做第二步判定:1. **判定**:若 $b'_i \bmod g \neq 0$,整个方程组**无解**,直接终止。2. **特解求解**:若 $g \mid b'_i$,则得到 $a_{k,i}x + my = g$ 的一组解 $x_0$,进而得到原方程在 $[0, m-1]$ 范围内的当前特解:
令 $g = \gcd(a_{k,i}, m)$,利用**扩展欧几里得**做第二步判定:1. **判定**:若 $b'_i \bmod g \neq 0$,整个方程组**无解**,直接停掉。2. **特解**:若 $g \mid b'_i$,则得到 $a_{k,i}x + my = g$ 的一组解 $x_0$,于是得到原方程在 $[0, m-1]$ 范围内的一个特解:$$x_i = \left( x_0 \times \frac{b'_i}{g} \pmod m + m \right) \pmod m$$
##### 解的形态与总解数
在确保方程组**有解**的前提下,整个方程组在模 $m$ 意义下的解的形态由自由元和主元共同决定:在确保方程组**有解**的前提下,整个方程组在模 $m$ 意义下的解的形态由自由元和主元决定:
* **唯一解**:秩 $r = n$(不存在自由变量),且对角线上的所有主元均满足 $\gcd(a_{pos[i],i}, m) = 1$(此时各行方程的 $g=1$,利用扩欧回代出唯一特解)。* **无穷解/多解**:秩 $r < n$(存在 $n - r$ 个完全自由变量);或者虽然 $r = n$,但至少存在一个主元满足 $\gcd(a_{pos[i],i}, m) > 1$。* **唯一解**:秩 $r = n$(不存在自由变量),且所有主元均满足 $\gcd(a_{k,p[k]}, m) = 1$(此时各行方程的 $g=1$,利用扩欧回代出唯一特解)。* **无穷解/多解**:秩 $r < n$(存在 $n - r$ 个完全自由变量);或者虽然 $r = n$,但至少存在一个主元满足 $\gcd(a_{k,p[k]}, m) > 1$。
**总解数**:
自底向上考虑:2 行未改动2. 每个**非自由变量**对应的同余方程 $a_{k,i}x_i \equiv b'_i \pmod m$ 在有解时,其在 $[0, m-1]$ 内的解数恰好为 $\gcd(a_{k,i}, m)$(各个解之间相差步长 $d = \frac{m}{g}$)。
因此,整个同余方程组的总解数为:
$$m^{n-r} \times \prod_{i \text{ 存在主元}} \gcd(a_{\text{pos}[i],i}, m)$$$$m^{n-r} \times \prod_{k=1}^r \gcd(a_{k,p[k]}, m)$$
时间复杂度 $O(n^3 \log m)$。
```cpp#include<iostream>#include<cstdio>#include<cmath>#define re register#define endl '\n'#define ll long long#define ull unsigned long long#define db double#define fi const int&#define fl const ll&#define ful const ull&#define fc const char&#define fs const string&#define debug() puts("I Love Hina Forever")using namespace std ;namespace IO { template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}} template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);} template<class T>inline void write(T x){if(x<0){putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);} template<class T>inline void write(T x,fc c){write(x),putchar(c);}}using namespace IO ;
const int N = 602,MOD = 998244353 ;int n,m,r = 1 ;int p[N] ;bool has_p[N] ;ll a[N][N],ans[N],cnt = 1 ;
inline ll exgcd (ll a,ll b,ll &x,ll &y) { if (!b) { x = 1,y = 0 ; return a ; } re ll g = exgcd(b,a % b,y,x) ; y -= a / b * x ; return g ;}
inline void gauss () { for (re int i = 1 ; i <= n ; ++i) { int mx = r ; for (re int k = r ; k <= n ; ++k) {116 行未改动全部展开
时间复杂度 $O(n^3)$。
```cpp#include<iostream>#include<cstdio>#define re register#define endl '\n'#define ll long long#define ull unsigned long long#define db double#define fi const int&#define fl const ll&#define ful const ull&#define fc const char&#define fs const string&#define debug() puts("I Love Hina Forever")using namespace std ;namespace IO { template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}} template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);} template<class T>inline void write(T x){if(x<0){putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);} template<class T>inline void write(T x,fc c){write(x),putchar(c);}}using namespace IO ;const int P = 1e9 + 7,N = 402 ;int n,p[N],r = 1 ;ll a[N][N << 1] ;inline ll qpow (ll a,ll b) {131 行未改动全部展开* **若 $r < n$**:说明消元后上三角矩阵的对角线上至少存在一个 $u_{i,i} = 0$,矩阵不满秩,其行列式的值直接为:
$$\det(A) \equiv 0 \pmod P$$
* **若 $r = n$**:说明矩阵满秩,给出未修正符号的绝对值 $ans$,其代数式为:* **若 $r = n$**:说明矩阵满秩,设绝对值 $ans$:
$$ans = \left( \prod_{i=1}^{n} a_{i,i} \right) \bmod P$$
最终的行列式值为:10 行未改动
代码:
```cpp#include<iostream>#include<cstdio>#define re register#define endl '\n'#define ll long long#define ull unsigned long long#define db double#define fi const int&#define fl const ll&#define ful const ull&#define fc const char&#define fs const string&#define debug() puts("I Love Hina Forever")using namespace std ;namespace IO { template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}} template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);} template<class T>inline void write(T x){if(x<0){putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);} template<class T>inline void write(T x,fc c){write(x),putchar(c);}}using namespace IO ;const int N = 602 ;int n,m,r = 1 ;ll a[N][N],ans = 1 ;bool f ;228 行未改动全部展开
求出第 $n$ 行后需要看第 $n$ 行(前面相邻两行会相互制约,但第 $n$ 行没有第 $n+1$ 行进行扭转纠正)最终颜色是否全 $0$,合法就统计 $x_{i,j} = 1$ 的数量。统计最小值。
```cpp#include<iostream>#include<cstdio>#include<bitset>#include<cstring>#define re register#define endl '\n'#define ll long long#define ull unsigned long long#define db double#define fi const int&#define fl const ll&#define ful const ull&#define fc const char&#define fs const string&#define debug() puts("I Love Hina Forever")using namespace std ;namespace IO { template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}} template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);} template<class T>inline void write(T x){if(x<0){putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);} template<class T>inline void write(T x,fc c){write(x),putchar(c);}}using namespace IO ;const int N = 18 ;int T,n ;int p[N * N],minn = 1e9 ;bool col[N][N],x[N * N] ;339 行未改动全部展开$$ans \gets \max(ans, ans \oplus a_i)$$
### Part3(对比):
这种写法与实数高斯消元的逻辑高度相似:
1. **实数消元**:利用减法消去未知数系数,目标是得到上三角矩阵。2. **异或消元**:利用异或消去二进制位上的 1,目标是得到每行最高位 1 呈阶梯状排列。
最后输出 `ans` 。
## [4. MarsOJ P2646. 【模板】Matrix-Tree 定理](https://marsoj.cn/p/P2641)这个是神秘行列式的应用。
### 内容
1. 生成树计数问题(相当于边权都为 $1$):给定无向联通图(有向图),求其生成树(外向生成树 / 内向生成树)的个数。1. 生成树计数(相当于边权都为 $1$):给定无向联通图(有向图),求其生成树(外向生成树 / 内向生成树)的个数。
- 生成树:对于一个无向连通图,从中选出一些边保留下来,如果构成了一棵树,则称其为该无向连通图的生成树。 - 外向树(内向树):从根指向叶子的树(从叶子指向根的树)。
158 行未改动全部展开$$ans = ans' \times \dfrac{1}{k}$$
也就是说高消的时候去归一某一行是乘的是某个数的**逆元**而非这个数(当然这是仅当模数是质数时才能用逆元直接算)。也就是说高消的时候去归一某一行,$ans'$ 乘的是某个数而非这个数的**逆元**(当然这是仅当模数是质数时才能用逆元直接算)。
```cpp/*_/ _/ _/_/_/_/_/_/ _/ _/ _/_/_/ _/ _/ _/_/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/ _/ _/_/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/_/_/_/_/_/ _/ _/ _/ _/*/#include<iostream>#include<cstdio>#define re register#define endl '\n'#define ll long long#define ull unsigned long long#define db double#define fi const int&#define fl const ll&#define ful const ull&#define fc const char&#define fs const string&#define debug() puts("I Love Hina Forever")using namespace std ;namespace IO { template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}} template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);} template<class T>inline void write(T x){if(x<0){putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);} template<class T>inline void write(T x,fc c){write(x),putchar(c);}}using namespace IO ;const int P = 1e9 + 7,N = 305 ;int n,m,t ;ll A[N][N],D[N][N] ;#define mod(x) ((((x) %= P) += P) %= P)194 行未改动全部展开
注意对 $p$ 取模。
```cpp/*_/ _/ _/_/_/_/_/_/ _/ _/ _/_/_/ _/ _/ _/_/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/ _/ _/_/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/_/_/_/_/_/ _/ _/ _/ _/*/#include<iostream>#include<cstdio>#define re register#define endl '\n'#define ll __int128#define ull unsigned long long#define db double#define fi const int&#define fl const ll&#define ful const ull&#define fc const char&#define fs const string&#define debug() puts("I Love Hina Forever")using namespace std ;namespace IO { template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}} template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);} template<class T>inline void write(T x){if(x<0){putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);} template<class T>inline void write(T x,fc c){write(x),putchar(c);}}using namespace IO ;ll n,m,p ;inline ll qpow (ll a,ll b) { ll ans = 1 ; while (b) {9 行未改动}```
## [6. MarsOJ P2649. Expectation](https://marsoj.cn/p/P2649)
定义生成树的“权值”为所有边权的按位与。
求随机一颗生成树权值期望。
### 思路
首先总情况数即总生成树个数 $sum$。
因为按位与对于每一个二进制位来说互不干涉,我们将边权拆为每一位去看。
把图中边权第 $i$ 个二进制位为 $1$ 的边提出来建一个新图(只有全 $1$ 才有贡献)求生成树个数 $now_i$,对应的值为 $2^i$。
期望 $p = \sum\limits_{i=1}^{base}\dfrac{now_i}{sum}\times 2^i$。
```cppconst int N = 102,P = 998244353 ;int T,n,m ;ll K[N][N],sum,ans ;struct node { int x,y ; ll z ;}e[N * N] ;#define mod(x) ((((x) %= P) += P) %= P)inline ll qpow (ll a,ll b) { re ll ans = 1 ; while (b) { if (b & 1) ans = ans * a % P ; b >>= 1,a = a * a % P ; } return ans ;}inline ll inv (fl x) {return qpow(x,P - 2) ;}inline ll gauss (ll a[N][N]) { int r = 1 ; bool f = 0 ; ll ans = 1 ; for (re int i = 1 ; i <= n && r <= n ; ++i) { int mx = r ; for (re int k = r ; k <= n ; ++k) { if (a[k][i]) { mx = k ; break ; } } if (!a[mx][i]) continue ; if (mx != r) f ^= 1 ; swap(a[mx],a[r]),mod(ans *= a[r][i]) ; ll t = inv(a[r][i]) ; for (re int j = n ; j >= i ; --j) mod(a[r][j] *= t) ; for (re int k = r + 1 ; k <= n ; ++k) { for (re int j = n ; j >= i ; --j) mod(a[k][j] -= a[k][i] * a[r][j]) ; } ++r ; } --r ; if (r < n) return 0 ; return f == 1 ? ((-ans % P) + P) % P : ans % P ;}inline void solve () { memset(K,0,sizeof K) ; for (re int i = 1,x,y,z ; i <= m ; ++i) { x = e[i].x,y = e[i].y,z = e[i].z ; ++K[x][x],++K[y][y],--K[x][y],--K[y][x] ; } for (re int i = 1 ; i <= n ; ++i) { for (re int j = 1 ; j <= n ; ++j) mod(K[i][j]) ; } --n ; sum = gauss(K) ; if (sum == 0) { ans = 0 ; return ; } sum = inv(sum) ; ++n ; for (re int bit = 0 ; bit < 30 ; ++bit) { memset(K,0,sizeof K) ; for (re int i = 1,x,y,z ; i <= m ; ++i) { x = e[i].x,y = e[i].y,z = e[i].z ; if ((z >> bit) & 1) ++K[x][x],++K[y][y],--K[x][y],--K[y][x] ; } for (re int i = 1 ; i <= n ; ++i) { for (re int j = 1 ; j <= n ; ++j) mod(K[i][j]) ; } --n ; mod(ans += gauss(K) * sum % P * (1ll << bit) % P) ; ++n ; }}int main () { read(T) ; while (T--) { read(n,m) ; int cnt = 0 ; for (re int i = 1,x,y,z ; i <= m ; ++i) { read(x,y,z) ; if (x == y) continue ; e[++cnt] = {x,y,z} ; } m = cnt ; solve() ; write(ans,endl) ; ans = sum = 0 ; } return 0 ;}```