正文
增加 495 行删除 124 行
89 行未改动90919293949596979899100101109 行未改动全部展开21121221321421521621721821922 行未改动24224324424524624724824925016 行未改动2672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023032 行未改动306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356116 行未改动全部展开47347447547647747847948048148248348448548648748848949049149249349449549649749849950060 行未改动56156256356456556656756862 行未改动63163263363463563663763863910 行未改动650651652653654655656657658659660661662663664665666667668669670671672673674675676677228 行未改动全部展开906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935339 行未改动全部展开12751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132289 行未改动在化简为行阶梯形后,我们从底向上观察:- **无解**:若存在某一行,其左边全为 $0$ 但右边不为 $0$,无解。- **唯一解**:不满足上述条件,且 $r$ 等于变量个数 $n$。- **无穷解**:不满足上述条件,且 $r$ 小于变量个数 $n$(即存在自由变量)。笼统的思路已经讲完了,下面进入例题讲解代码。### $n$ 元 $1$ 次异或方程组形如109 行未改动全部展开对于一般情况,其对应的模 $m$ 增广矩阵为:$$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$ 倍。由于涉及减法,需做防负数取模处理:$$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`)** **情况 A:`pos[i] == 0`(自由变量)**在模 $m$ 意义下有 $m$ 种取值方案。若构造特解直接令:$$x_i = 0$$* **情况 B:`pos[i] != 0`(非自由变量,设 $k = \text{pos}[i]$)**将已求出的高位未知数 $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]$ 范围内的当前特解:$$x_i = \left( x_0 \times \frac{b'_i}{g} \pmod m + m \right) \pmod 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$。**总解数**:自底向上考虑: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)$$时间复杂度 $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) {60 行未改动#### 1. 行列式求值的方法根据行列式的初等变换性质:* 交换矩阵的两行,行列式的值取反。若记某次变换为 $A \xrightarrow{r_i \leftrightarrow r_j} A'$,则 $\det(A') = -\det(A)$。* 将矩阵某行的 $k$ 倍加到另一行,行列式的值不变。即 $A \xrightarrow{r_i \gets r_i + k \cdot r_j} A'$,则 $\det(A') = \det(A)$。* 将矩阵某行乘 $k$,行列式的值也乘 $k$。$A \xrightarrow{r_i \gets k\times r_i} A'$,则 $\det(A') = k\times\det(A)$。62 行未改动* **若 $r < n$**:说明消元后上三角矩阵的对角线上至少存在一个 $u_{i,i} = 0$,矩阵不满秩,其行列式的值直接为:$$\det(A) \equiv 0 \pmod P$$* **若 $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. 生成树计数问题:给定无向联通图(有向图),求其生成树(内向生成树 / 外向生成树)的个数。- 生成树:对于一个无向连通图,从中选出一些边保留下来,如果构成了一棵树,则称其为该无向连通图的生成树。- 内向树(外向树):从叶子指向根的树(从根指向叶子的树)。2. 定义其一个生成树 $T$ 的权值为 $T$ 中所有边权的乘积,求其所有不同生成树的权值之和(有 / 无向图)。(就是本题)### 解法三种情况的大致思路相同:1. 求出矩阵 $D,A$。2. 算 $K = D - A$。3. 设根为 $r$,则去掉 $K$ 的第 $r$ 行和第 $r$ 列,得出一个 $[n-1][n-1]$ 的矩阵,算出这个矩阵的行列式。区别在于 $D,A$ 矩阵的定义。#### 无向图对于一个 $n$ 个点的无向图 $G$,我们定义 $D$ 为度数矩阵:$$D_{i,j} =\begin{cases}0,&i\not=j\\\sum\limits\end{cases}$$89 行未改动90919293949596979899100101109 行未改动全部展开21121221321421521621721821922022 行未改动24324424524624724824925025116 行未改动2682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982992 行未改动302303304305306307308309310311312313314315316317318319320321322323324325326327328116 行未改动全部展开44544644744844945045145260 行未改动51351451551651751851952052162 行未改动58458558658758858959059159210 行未改动603604605606607608609610228 行未改动全部展开839840841842843844845846339 行未改动全部展开118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169389 行未改动在化简为行阶梯形后,我们从底向上观察:- **无解**:若存在某一行,其左边全为 $0$ 但右边不为 $0$,无解。- **唯一解**:不满足无解,且 $r$ 等于变量个数 $n$。- **无穷解**:不满足无解,且 $r$ 小于变量个数 $n$(即存在自由变量)。笼统的思路已经讲完了,在例题讲代码。### $n$ 元 $1$ 次异或方程组形如109 行未改动全部展开对于一般情况,其对应的模 $m$ 增广矩阵为:$$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$ 倍。由于涉及减法,需做防负数取模:$$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. 回代与第二步判定(自底向上)第一步判定后,我们进行回代求特解或进行第二步判定。*(令 `p[k]` 为第 $k$ 行的主元所在列号,用 `has_p[i]` 记录第 $i$ 列是否存在主元)** **自由变量(`has_p[i] == 0`)**遍历 $i \in [1, n]$,若该列不存在主元,则 $x_i$ 为自由变量,在模 $m$ 意义下有 $m$ 种取值方案。构造特解直接令:$$x_i = 0$$* **非自由变量(自底向上遍历行,从 $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)$,利用**扩展欧几里得**做第二步判定: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$ 意义下的解的形态由自由元和主元决定:* **唯一解**:秩 $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_{k=1}^r \gcd(a_{k,p[k]}, m)$$时间复杂度 $O(n^3 \log m)$。```cppconst 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)$。```cppconst int P = 1e9 + 7,N = 402 ;int n,p[N],r = 1 ;ll a[N][N << 1] ;inline ll qpow (ll a,ll b) {60 行未改动#### 1. 行列式求值的方法根据行列式的初等变换性质:* 若行列式 $|T|$ 可以写成 $$\begin{vmatrix}A&B\\C&D\end{vmatrix}$$(其中 $A,D$ 为方阵,$B$ 或 $C$ 为全 $0$ 矩阵),那么 $|T|=|A|\times|D|$。* 交换矩阵的两行,行列式的值取反。若记某次变换为 $A \xrightarrow{r_i \leftrightarrow r_j} A'$,则 $\det(A') = -\det(A)$。* 将矩阵某行的 $k$ 倍加到另一行,行列式的值不变。即 $A \xrightarrow{r_i \gets r_i + k \cdot r_j} A'$,则 $\det(A') = \det(A)$。* 将矩阵某行乘 $k$,行列式的值也乘 $k$。$A \xrightarrow{r_i \gets k\times r_i} A'$,则 $\det(A') = k\times\det(A)$。62 行未改动* **若 $r < n$**:说明消元后上三角矩阵的对角线上至少存在一个 $u_{i,i} = 0$,矩阵不满秩,其行列式的值直接为:$$\det(A) \equiv 0 \pmod P$$* **若 $r = n$**:说明矩阵满秩,设绝对值 $ans$:$$ans = \left( \prod_{i=1}^{n} a_{i,i} \right) \bmod P$$最终的行列式值为:10 行未改动代码:```cppconst 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$ 的数量。统计最小值。```cppconst 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)$$最后输出 `ans` 。## [4. MarsOJ P2646. 【模板】Matrix-Tree 定理](https://marsoj.cn/p/P2641)这个是神秘行列式的应用。### 内容1. 生成树计数(相当于边权都为 $1$):给定无向联通图(有向图),求其生成树(外向生成树 / 内向生成树)的个数。- 生成树:对于一个无向连通图,从中选出一些边保留下来,如果构成了一棵树,则称其为该无向连通图的生成树。- 外向树(内向树):从根指向叶子的树(从叶子指向根的树)。2. 定义其一个生成树 $T$ 的“权值”为 $T$ 中所有边权的乘积,求其所有不同生成树的“权值”之和(有 / 无向图)。(就是本题)### 解法(直接讲第二个,第一个严格被包含)三种情况的大致思路相同:1. 求出矩阵 $D,A$。2. 算 $K = D - A$。3. 设根为 $r$,则去掉 $K$ 的第 $r$ 行和第 $r$ 列得到 $K'$,算出 $K'$ 的行列式。区别在于 $D,A$ 矩阵的定义。#### 无向图对于一个 $n$ 个点的无向图 $G$,##### 1.我们定义 $D$ 为度数矩阵:$$D_{i,j} =\begin{cases}0,&i\not=j\\\sum\limits_\text{连着 i 的边权},&i=j\end{cases}$$特别地,当为第一种情况时,$D$ 的形式:$$D_{i,j} =\begin{cases}0,&i\not=j\\\text{i 的度数},&i=j\end{cases}$$定义 $A$ 为邻接矩阵:$$A_{i,j} =\begin{cases}\sum\limits_\text{连着 i 和 j 的边权},&i\not=j\\0,&i=j\end{cases}$$特别地,当为第一种情况时,$A$ 的形式:$$A_{i,j} =\begin{cases}\text{连着 i 和 j 的边数},&i\not=j\\0,&i=j\end{cases}$$##### 2. 算 $K = D - A$。##### 3.因为无向图没有特殊指定根(哪个点当根都行),我们对 $K$ 随便去掉一行一列(一般是最后一行最后一列)去算 $|K'|$ 即可。(这是无向图具有的性质,随便去掉一行一列的行列式相同)#### 有向图##### 1.对于一个 $n$ 个点的无向图 $G$,我们定义 $D$ 为度数矩阵(对于外向树统计**入度矩阵** $D_{in}$,对于内向树统计**出度矩阵** $D_{out}$):**对于外向树**:$$D_{in_{i,j}} =\begin{cases}0,&i\not=j\\\sum\limits_\text{连向 i 的边权},&i=j\end{cases}$$特别地,当为第一种情况时,$D$ 的形式:$$D_{in_{i,j}} =\begin{cases}0,&i\not=j\\\text{i 的入度},&i=j\end{cases}$$**对于内向树**:$$D_{out_{i,j}} =\begin{cases}0,&i\not=j\\\sum\limits_\text{从 i 出去的边权},&i=j\end{cases}$$特别地,当为第一种情况时,$D$ 的形式:$$D_{out_{i,j}} =\begin{cases}0,&i\not=j\\\text{i 的出度},&i=j\end{cases}$$相当于对于边 $(x,y,z)$,- 如果是外向树,则 $z$ 对 $D_{y,y}$ 有贡献(说明了 $D$ 是算**入度**的)。- 如果是内向树,则 $z$ 对 $D_{x,x}$ 有贡献(说明了 $D$ 是算**出度**的)。定义 $A$ 为邻接矩阵(邻接矩阵就是用来表示图的,所以内向树与外向树是一样的):$$A_{i,j} =\begin{cases}\sum\limits_\text{ i 连向 j 的边权},&i\not=j\\0,&i=j\end{cases}$$特别地,当为第一种情况时,$A$ 的形式:$$A_{i,j} =\begin{cases}\text{ i 连向 j 的边数},&i\not=j\\0,&i=j\end{cases}$$##### 2. 算 $K = D - A$。##### 3.设根为 $r$,则求出 $K$ 去掉第 $r$ 行第 $r$ 列的 $|K'|$。**注意**:重边对计算没有影响,但要忽略自环(两个矩阵都要忽略!)这里说一个求行列式的注意事项:如果我们求 $|A|$,要将 $A$ 消成行阶梯的 $A'$。设 $ans = |A|,ans' = |A'|$。如果我们将 $A$ 的某一行 $\times k$ 变成 $A'$,那么 $ans$ 与 $ans'$ 的关系:$$ans \times k = ans'$$然而我们求的是 $ans$,且我们已经通过乘对角线算出了 $ans'$,所以:$$ans = ans' \times \dfrac{1}{k}$$也就是说高消的时候去归一某一行,$ans'$ 乘的是某个数而非这个数的**逆元**(当然这是仅当模数是质数时才能用逆元直接算)。```cppconst int P = 1e9 + 7,N = 305 ;int n,m,t ;ll A[N][N],D[N][N] ;#define mod(x) ((((x) %= P) += P) %= P)inline ll qpow (ll a,ll b) {ll ans = 1 ;while (b) {if (b & 1) ans = ans * a % P ;b >>= 1,a = a * a % P ;}return ans ;}inline ll inv (ll x) {return qpow(x,P - 2) ;}inline void gauss () {ll ans = 1 ;bool f = 0 ;int r = 1 ;for (re int i = 1 ; i <= n ; ++i) {int mx = r ;for (re int k = r ; k <= n ; ++k) {if (A[mx][i] < A[k][i]) mx = k ;}if (!A[mx][i]) continue ;if (mx != r) swap(A[mx],A[r]),f ^= 1 ;ll t = inv(A[r][i]) ;mod(ans *= 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) {write(0) ;return ;}for (re int i = 1 ; i <= n ; ++i) (ans *= A[i][i]) %= P ;write(f ? ((-ans % P) + P) % P : ans % P) ;}inline void solve1 () {for (re int i = 1,u,v,w ; i <= m ; ++i) {read(u,v,w) ;if (u == v) continue ;A[u][v] += w,A[v][u] += w ;}for (re int i = 1 ; i <= n ; ++i) {for (re int j = 1 ; j <= n ; ++j) D[i][i] += A[i][j] ;}for (re int i = 1 ; i <= n ; ++i) {for (re int j = 1 ; j <= n ; ++j) mod(A[i][j] = D[i][j] - A[i][j]) ;}--n ;gauss() ;}inline void solve2 () {for (re int i = 1,u,v,w ; i <= m ; ++i) {read(u,v,w) ;if (u == v) continue ;A[u][v] += w ;}for (re int i = 1 ; i <= n ; ++i) {for (re int j = 1 ; j <= n ; ++j) D[i][i] += A[j][i] ;}for (re int i = 1 ; i <= n ; ++i) {for (re int j = 1 ; j <= n ; ++j) mod(D[i][j] -= A[i][j]) ;}for (re int i = 1 ; i < n ; ++i) {for (re int j = 1 ; j < n ; ++j) A[i][j] = D[i + 1][j + 1] ;}--n ;gauss() ;}int main () {read(n,m,t) ;if (t == 0) solve1() ;else solve2() ;return 0 ;}```## [5. MarsOJ P2733. 文艺计算姬](https://marsoj.cn/p/P2733)依旧神仙题。题目即**求解带标号完全二分图 $K_{n,m}$ 的生成树个数,结果对 $p$ 取模。****二分图**:对于图 $G$,若能将其点集 $V$ 划分为两个不相交的非空子集 $A$ 和 $B$,使得图中的每条边所连接的两个顶点分别属于这两个不同的点集(即点集内部没有连边),则称 $G$ 为二分图。**完全二分图**:对于二分图 $G$,设其分成的两个点集为 $A$ 和 $B$。若对于任意的 $x \in A$ 和 $y \in B$,图中都存在一条边连接 $x$ 和 $y$,则称 $G$ 为完全二分图(若 $A$ 的大小为 $m$,$B$ 的大小为 $n$,可记作 $K_{m,n}$)。对于“普通计算机”,求的是完全图的生成树数量:$ans = n^{n-2}$(Cayley 公式)。而对于我们要求的即为扩展 Cayley 公式(对于 $K_{n,m}$):$ans = n^{m-1} \times m^{n-1}$。这个可以用 Matrix-Tree 推导。### 推导对于 $D$,其形式为:$$D = \begin{bmatrix}m & & & & & \\& \ddots & & & & \\& & m & & & \\& & & n & & \\& & & & \ddots & \\& & & & & n\end{bmatrix}_{(n+m) \times (n+m)}=\begin{bmatrix} m \cdot I_n & O_{n\times m} \\ O_{m\times n} & n \cdot I_m \end{bmatrix}$$对于 $A$,其形式为:$$A = \begin{bmatrix}0 & \cdots & 0 & 1 & \cdots & 1 \\\vdots & \ddots & \vdots & \vdots & \ddots & \vdots \\0 & \cdots & 0 & 1 & \cdots & 1 \\1 & \cdots & 1 & 0 & \cdots & 0 \\\vdots & \ddots & \vdots & \vdots & \ddots & \vdots \\1 & \cdots & 1 & 0 & \cdots & 0\end{bmatrix}_{(n+m) \times (n+m)}=\begin{bmatrix} O_n & J_{n \times m} \\ J_{m \times n} & O_m \end{bmatrix}$$那么 $K = D - A = \begin{pmatrix} m \cdot I_n & -J_{n \times m} \\ -J_{m \times n} & n \cdot I_m \end{pmatrix}$。我们去掉 $K$ 的最后一行最后一列,得到 $K'$。$$|K'| = \begin{vmatrix}m \cdot I_n & -J_{n \times (m-1)} \\-J_{(m-1) \times n} & n \cdot I_{m-1}\end{vmatrix}$$我们将(上面每一行相加再 $\times \dfrac{1}{m}$)加到下面每一行上(相当于 $t = \sum\limits_{i=1}^nK'_i,K'_j\gets K'_j+\dfrac{t}{m}(j\in[n+1,n+m-1])$),就是利用前 $n$ 行,将左下角的 $-J_{(m-1) \times n}$ 消为 $O$:$$|K'| = \left| \begin{array}{ccc|ccc}m & \cdots & 0 & -1 & \cdots & -1 \\\vdots & \ddots & \vdots & \vdots & \ddots & \vdots \\0 & \cdots & m & -1 & \cdots & -1 \\ \hline0 & \cdots & 0 & n-\frac{n}{m} & \cdots & -\frac{n}{m} \\\vdots & \ddots & \vdots & \vdots & \ddots & \vdots \\0 & \cdots & 0 & -\frac{n}{m} & \cdots & n-\frac{n}{m}\end{array} \right|$$于是 $|K'|=$ 左上 $\times$ 右下:$$|K'| = |m \cdot I_n| \times \left| \begin{array}{cccc}n-\frac{n}{m} & -\frac{n}{m} & \cdots & -\frac{n}{m} \\-\frac{n}{m} & n-\frac{n}{m} & \cdots & -\frac{n}{m} \\\vdots & \vdots & \ddots & \vdots \\-\frac{n}{m} & -\frac{n}{m} & \cdots & n-\frac{n}{m}\end{array} \right|_{(m-1) \times (m-1)}=m^n \times\left| \begin{array}{cccc}n-\frac{n}{m} & -\frac{n}{m} & \cdots & -\frac{n}{m} \\-\frac{n}{m} & n-\frac{n}{m} & \cdots & -\frac{n}{m} \\\vdots & \vdots & \ddots & \vdots \\-\frac{n}{m} & -\frac{n}{m} & \cdots & n-\frac{n}{m}\end{array} \right|_{(m-1) \times (m-1)}$$对于右侧那一坨我们设为 $|T|$,我们将第 $2$ 行到第 $m-1$ 行都加到第 $1$ 行上($t = \sum\limits_{i=2}^{m-1}T_i,T_1\gets T_1+t$):$$|T|=\begin{vmatrix}\frac{n}{m} & \frac{n}{m} & \cdots & \frac{n}{m} \\-\frac{n}{m} & n-\frac{n}{m} & \cdots & -\frac{n}{m} \\\vdots & \vdots & \ddots & \vdots \\-\frac{n}{m} & -\frac{n}{m} & \cdots & n-\frac{n}{m}\end{vmatrix}$$我们将第 $1$ 行加到下面所有行上($T_i\gets T_i+T_1(i\in[2,m-1])$):$$|T|=\begin{vmatrix}\frac{n}{m} & \frac{n}{m} & \cdots & \frac{n}{m} \\0 & n & \cdots & 0 \\\vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & n\end{vmatrix} = \frac{n}{m} \times n^{m-2}$$那么最终,$|K'|=m^n\times(\dfrac{n}{m}\times n^{m-2})=m^{n-1}\times n^{m-1}$。注意对 $p$ 取模。```cppll n,m,p ;inline ll qpow (ll a,ll b) {ll ans = 1 ;while (b) {if (b & 1) ans = ans * a % p ;b >>= 1,a = a * a % p ;}return ans ;}int main () {read(n,m,p) ;write(qpow(n,m - 1) * qpow(m,n - 1) % p) ;return 0 ;}```## [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 ;}```