跳到正文

题解

题解:数据生成器

喵仔牛奶约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年9月1日
收录
2026年9月14日
更新
2026年11月11日
篇幅
约 10 分钟
获赞
5
题目
P17410

版本

  1. v1当前

小 A 出的题是经典的网络流问题。每行每列建点,源点连向行,列连向汇点,容量都为可以填的 00 的个数。对于 ci,j=2c_{i,j}=2,连边 i→ji\to j,容量为 11。最大流即为答案中 00 的个数。

考虑最优解唯一的充要条件,将给出的最优解对应到建图中,条件显然是残量网络中 SS 不可达 TT 且不存在环(SS 到行再回来的环除外)。连边 T→ST\to S,限制统一为不存在环。

存在行到 SS 的反向边的条件是这行流了至少一个 11,注意到如果这行不存在 11,加上这条反向边也不会产生环。因此令行到 SS 和列到 TT 的边永远存在。

此时的残量网络就是:如果一行的限制没有满则 SS 和这行连无向边,否则连行到 SS 的边,列同理;如果原始矩阵中 (i,j)(i,j) 填 22,若 di,j=1d_{i,j}=1 连 i→ji\to j,否则连 j→ij\to i。

直接 DAG 容斥即可,有无向边的 DAG 容斥可以参考 ABC306Ex。复杂度 O(3n+m)\mathcal O(3^{n+m})。

C++
#include <bits/stdc++.h>
#define REP(i, l, r) for (int i = (l); i <= (r); ++ i)
#define DEP(i, r, l) for (int i = (r); i >= (l); -- i)
#define CUP(i, l, r) for (int i = (l); i < (r); ++ i)
#define fi first
#define se second
#define pb emplace_back
#define mems(x, v) memset((x), (v), sizeof(x))
#define memc(x, y) memcpy((x), (y), sizeof(x))
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define ppc(x) __builtin_popcount(x)
#define ctz(x) __builtin_ctz(x)
#define clz(x) __builtin_clz(x)
using namespace std;
namespace math {
	template <class T> T qpow(T a, long long b) { if (!b) return {1}; T c = a; for (b --; b; b >>= 1, a = a * a) if (b & 1) c = c * a; return c; }
	template <unsigned P = 0> struct mint {
		unsigned v; static unsigned M; mint() = default;
		constexpr static unsigned mod() { return P > 0 ? P : M; } static void set(unsigned m) { M = m; }
		template <class T> mint(T x) { x %= (int)mod(), v = x < 0 ? x + mod() : x; }
		mint operator + () const { return *this; } mint operator - () const { return 0 - *this; }
		mint inv() const { return assert(v), qpow(*this, mod() - 2); } int val() const { return v; }
		mint &operator += (const mint &q) { (v += q.v, v >= mod()) && (v -= mod()); return *this; }
		mint &operator -= (const mint &q) { (v -= q.v, v >= mod()) && (v += mod()); return *this; }
		mint &operator *= (const mint &q) { v = 1ull * v * q.v % mod(); return *this; }
		mint &operator /= (const mint &q) { return *this *= q.inv(); }
		#define O1(o) friend mint operator o (mint p, const mint &q) { return p o##= q; }
		#define O2(o) friend bool operator o (const mint &p, const mint &q) { return p.v o q.v; }
		O1(+) O1(-) O1(*) O1(/) O2(==) O2(!=) O2(<) O2(>) O2(<=) O2(>=)
		friend istream &operator >> (istream &is, mint &a) { long long x; is >> x, a = x; return is; }
		friend ostream &operator << (ostream &os, const mint &a) { os << a.v; return os; }
	};
	template <> unsigned mint<0>::M = 998244353;
	template <typename MI> struct comb {
		vector<MI> f, g;
		void init(int n) {
			n = min(n, (int)MI::mod() - 1), f.resize(n + 1), g.resize(n + 1), f[0] = 1;
			REP(i, 1, n) f[i] = f[i - 1] * i; g[n] = f[n].inv(); DEP(i, n, 1) g[i - 1] = g[i] * i;
		}
		MI operator () (long long n, long long m) {
			if (m > n || m < 0 || n < 0) return 0;
			if (n < MI::mod() && m < MI::mod()) return assert(n < SZ(f)), f[n] * g[m] * g[n - m];
			return (!n ? 1 : (*this)(n / MI::mod(), m / MI::mod()) * (*this)(n % MI::mod(), m % MI::mod()));
		}
	};
	namespace Z {
		const int mod = 998244353;
		using MI = mint<mod>; using math::qpow; comb<MI> C;
		MI fac(int x) { assert(x >= 0 && x < SZ(C.f)); return C.f[x]; }
		MI ifc(int x) { assert(x >= 0 && x < SZ(C.g)); return C.g[x]; }
		MI inv(int x) { assert(x && x < SZ(C.f)); return C.g[x] * C.f[x - 1]; }
		MI sab(int x, int y) { return (!y ? !x : C(x + y - 1, y - 1)); }
	}
}
namespace Milkcat {
	using namespace math::Z;
	typedef long long LL;
	typedef pair<LL, LL> pii;
	const int N = 8, M = 1 << N;
	int n, m, A[N], B[N], c[N][N], d[N][N], h3[N][M], t3[M]; char x;
	MI c1[M], c2[M], h1[N][M], h2[N][M], t1[M], t2[M], f[M][M], g[M];
	int main() {
		cin >> n >> m;
		CUP(i, 0, n) CUP(j, 0, m)
			cin >> x, c[i][j] = x - '0';
		CUP(i, 0, n) CUP(j, 0, m)
			cin >> x, x -= '0', d[i][j] = x, A[i] += x, B[j] += x;
		CUP(i, 0, n) CUP(j, 0, m)
			c[i][j] = (c[i][j] & 4) >> 1 | (c[i][j] >> d[i][j] & 1);
		CUP(S, 0, 1 << n)
			c1[S] = (!S ? -1 : -c1[S & (S - 1)] * A[ctz(S)]);
		CUP(S, 0, 1 << m)
			c2[S] = (!S ? -1 : -c2[S & (S - 1)] * B[ctz(S)]);
		CUP(x, 0, n) CUP(S, 0, 1 << m) {
			int y = ctz(S);
			h1[x][S] = (!S ? 1 : h1[x][S & (S - 1)] * ((c[x][y] & 1) + (c[x][y] >> 1 & d[x][y])));
		}
		CUP(x, 0, m) CUP(S, 0, 1 << n) {
			int y = ctz(S);
			h2[x][S] = (!S ? 1 : h2[x][S & (S - 1)] * ((c[y][x] & 1) + (c[y][x] >> 1 & !d[y][x])));
			h3[x][S] = (!S ? 1 : h3[x][S & (S - 1)] & (c[y][x] & 1));
		}
		CUP(S, 0, 1 << n) CUP(T, 0, 1 << m)
			for (int U1 = S; U1 < (1 << n); U1 = (U1 + 1) | S) {
				int P = S ^ U1;
				t1[P] = (!P ? 1 : t1[P & (P - 1)] * h1[ctz(P)][T]);
				for (int U2 = T; U2 < (1 << m); U2 = (U2 + 1) | T) {
					int Q = T ^ U2;
					t2[Q] = (!Q ? 1 : t2[Q & (Q - 1)] * h2[ctz(Q)][S]);
					t3[Q] = (!Q ? 1 : t3[Q & (Q - 1)] & h3[ctz(Q)][P]);
					MI w = t1[P] * t2[Q] * t3[Q] * ((ppc(P) + ppc(Q) & 1) ? 1 : -1);
					if (P || Q) f[S | P][T | Q] += f[S][T] * w;
					if (!S) f[S | P][T | Q] += w * c1[P];
					if ((T | Q) == (1 << m) - 1) {
						if (P && !Q) g[S | P] += g[S] * w;
						g[S | P] += f[S][T] * w * c2[Q];
					}
				}
			}
		cout << g[(1 << n) - 1] << '\n';
		return 0;
	}
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	int T = 1;
	while (T --) Milkcat::main();
	return 0;
}
原文