计数小记
行列式求值
一个行列式
这里的
- 一个行列式交换两行/两列后,行列式的权值变成相反数。证明:这等价于证一个排列交换任意两个数的逆序对个数的奇偶性会变化,而这是简单的。
- 一个行列式中一行同时乘
k ,那么权值的变化也是乘k 。 -
- 如果有
A 存在一个i,j 满足,存在一个定值k 使A_{i,x}=kA_{j,x} ,那么det(A)=0 。证明就是,先把A 的第j 行乘k ,那么有第i 行和第j 行相同,所以任意交换第i,j 行的det 相同,所以权值只能是0 。 - 把第
i 行的k 倍加到第j 行,行列式的权值不变。证明用(3),(4) 即可。
那么我们只需高斯消元做到
这个写成辗转相除是因为
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
using namespace std;
const int maxn = 605;
int n, a[maxn][maxn], mod;
int solve(){
int w = 1;
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
while(a[i][i]){
int k = a[j][i] / a[i][i];
for(int x = i; x <= n; x++) a[j][x] = (a[j][x] - 1ll * k * a[i][x] % mod + mod) % mod;
swap(a[i], a[j]); w *= -1;
}
swap(a[i], a[j]); w *= -1;
}
}
int s = 1; for(int i = 1; i <= n; i++) s = 1ll * s * a[i][i] % mod;
return (s * w + mod) % mod;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> mod;
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) cin >> a[i][j];
cout << solve();
return 0;
}
Matrix-Tree 定理
对于无向图,设
对于有向图,设
如果
这个如果忘了可以手推小样例看是哪个。
带权就是把
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
using namespace std;
const int maxn = 305;
const int mod = 1e9 + 7;
int n, m, t, a[maxn][maxn];
void Add(int &x, int y){((x += y) >= mod) && (x -= mod);}
int get(){
int N = n - 1, w = 1;
for(int i = 1; i <= N; i++){
for(int j = i + 1; j <= N; j++){
while(a[i][i]){
int k = a[j][i] / a[i][i];
for(int x = i; x <= N; x++) a[j][x] = (a[j][x] - 1ll * a[i][x] * k % mod + mod) % mod;
swap(a[i], a[j]); w *= -1;
}
swap(a[i], a[j]); w *= -1;
}
}
int s = 1; for(int i = 1; i <= N; i++) s = 1ll * s * a[i][i] % mod;
return (s * w + mod) % mod;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> t;
for(int i = 1; i <= m; i++){
int u, v, w; cin >> u >> v >> w;
if(!t){
Add(a[u][v], mod - w); Add(a[v][u], mod - w); Add(a[u][u], w); Add(a[v][v], w);
}else{
Add(a[u][v], mod - w); Add(a[v][v], w);
}
}
for(int i = 2; i <= n; i++) for(int j = 2; j <= n; j++) a[i - 1][j - 1] = a[i][j];
cout << get() << '\n';
return 0;
}
BEST 定理
对于一个有向欧拉图,其欧拉回路数量为:
其中
所以直接跑一个矩阵树定理就行。
下面是 模板 的代码,这个与正常的板子的区别是固定了以
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
using namespace std;
const int maxn = 105;
const int mod = 1e6 + 3;
const int maxm = 3141592 + 5;
int n, a[maxn][maxn], in[maxn], out[maxn], tot, idx[maxn], b[maxn][maxn], fac[maxm];
void init(){
for(int i = 1; i <= n; i++){
in[i] = out[i] = 0;
memset(a[i], 0, sizeof(int) * (n + 1));
}
}
int qpow(int x, int y){int sum = 1; for(; y; y >>= 1, x = 1ll * x * x % mod) if(y & 1) sum = 1ll * sum * x % mod; return sum;}
int get(){
int w = 1;
for(int i = 1; i < tot; i++){
int pos = 0;
for(int j = i; j < tot; j++) if(a[j][i]){pos = j; break;}
if(!pos) return 0;
if(pos ^ i) swap(a[i], a[pos]), w = -w;
for(int j = i + 1; j < tot; j++){
int k = 1ll * a[j][i] * qpow(a[i][i], mod - 2) % mod;
for(int x = i; x < tot; x++){
a[j][x] = (a[j][x] - 1ll * k * a[i][x] % mod + mod) % mod;
}
}
}
int s = 1; for(int i = 1; i < tot; i++) s = 1ll * s * a[i][i] % mod;
return (s * w + mod) % mod;
}
void solve(){
init();
cin >> n;
int s = 0;
for(int i = 1; i <= n; i++){
int x; cin >> x; s += x;
while(x--){
int y; cin >> y;
a[i][y]--;
a[y][y]++;
in[y]++; out[i]++;
}
}
if(!s){
cout << "1\n";
return ;
}
for(int i = 1; i <= n; i++) if(in[i] != out[i]){
cout << "0\n"; return ;
}
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) a[i][j] = (a[i][j] + mod) % mod;
tot = 0;
for(int i = 1; i <= n; i++) if(in[i]) idx[++tot] = i;
for(int i = 1; i <= tot; i++) for(int j = 1; j <= tot; j++) b[i][j] = a[idx[i]][idx[j]];
for(int i = 1; i <= tot; i++) for(int j = 1; j <= tot; j++) a[i][j] = b[i][j];
int sum = get();
for(int i = 1; i <= tot; i++) sum = 1ll * sum * fac[in[idx[i]] - 1] % mod;
sum = 1ll * sum * in[1] % mod;
cout << sum << '\n';
}
void Init(){
int N = 3141592;
fac[0] = 1; for(int i = 1; i <= N; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
Init();
int t; cin >> t; while(t--) solve();
return 0;
}