Small but Funny Tricks [Remember them all!]

  1. 模数 1e9 的神奇求行列式:
#include <bits/stdc++.h>

using namespace std; const int maxn = 1e2, mod = 1e9;

#define n N

int n, a[maxn][maxn]; inline int det()
{
	//cout << n << endl;
	int ans = 1; for (int i = 1; i <= n; i++)
	{
		for (int k = i + 1; k <= n; k++) while (a[k][i])
		{
			int d = a[i][i] / a[k][i];
			for (int j = i; j <= n; j++) a[i][j] = (a[i][j] - 1ll * d * a[k][j] % mod + mod) % mod;
			swap(a[i], a[k]), ans *= -1;
		}
		ans = (ans * 1ll * a[i][i]) % mod/*, cout << ans << endl*/;
	}
	return (ans % mod + mod) % mod;
}

#undef n

inline void add(int u, int v) { a[u][v]--, a[v][u]--, a[u][u]++, a[v][v]++; /*cout << 'H' << u << v << endl;*/ }

char s[maxn][maxn]; int n, m, num[maxn][maxn]; int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cin >> n >> m;
	for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> s[i][j]; if (s[i][j] == '.') num[i][j] = ++N; }
	for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (num[i][j])
	{
		if (num[i][j + 1]) add(num[i][j], num[i][j + 1]); if (num[i + 1][j]) add(num[i][j], num[i + 1][j]);
	}
	N--, cout << det() << endl;
}
  1. 有标号联通无向图计数中的这个卷积:

\[\frac{f_n}{(n-1)!}=\sum_{i=1}^n \frac{g_i}{(i-1)!} \cdot \frac{f_{n-i}}{(n-i)!} \]

可以导出 \(A=B\times C\) 从而推出 \(B= A\times C^{-1}\), 因为 \((-1)!=+\infty\),所以上面的式子可以改写为从 0 开始求和(当然还是整数的方程辣),并且 \(C_0=1\), \(C\) 存在逆元.

posted @ 2021-02-14 21:01  Linshey  阅读(106)  评论(0编辑  收藏  举报