版本比较
勰码金牌学院 S1 课程 16&17. 高斯消元
正文
增加 108 行删除 144 行
306 行未改动全部展开307308309310311312313314315316317318319320321322323324325326327328329330331332311312313314315338316317318319320321322323324348325326327328116 行未改动全部展开445446447448473474475476477478479480481482483484485486487488489490491492449450451452150 行未改动全部展开603604605606651652653654655656657658659660661662663664665666667668669670607608609610228 行未改动全部展开839840841842907908909910911912913914915916917918919920921922923924925926927928843844845846516 行未改动全部展开1363136413651366145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811367136813691370194 行未改动全部展开15651566156715681684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171215691570157115729 行未改动1582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693306 行未改动全部展开
时间复杂度 $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) {150 行未改动全部展开
代码:
```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] ;516 行未改动全部展开
也就是说高消的时候去归一某一行,$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 ;}```