跳到正文

题解

P9841 [ICPC 2021 Nanjing R] Puzzle in Inazuma

EuphoricStar约 11 分钟
原文

这是 2026年3月27日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年3月27日
更新
2026年3月27日
篇幅
约 11 分钟
题目
P9841

版本

  1. v2当前
  2. v1

构造还是 2 hard 4 me。

首先令 ai,j=wi,j−vi,ja_{i, j} = w_{i, j} - v_{i, j},目标变成让 aa 清零。然后发现边权和不变,所以若边权和 ≠0\ne 0 则无解。

然后这种构造的一个通解应该是,组合一些题目给出的操作,得到较为简单的操作。

初步的观察是,进行 (a,b,c,d,x)(a, b, c, d, x) 和 (b,a,c,d,x)(b, a, c, d, x),可以让 (a,b)(a, b) 边权加上 2x2x,(c,d)(c, d) 边权减去 2x2x。

进一步地,若 n≥5n \ge 5,再找一个点 ee,先让 (a,b)(a, b) 加 2x2x,(d,e)(d, e) 减 2x2x,再让 (a,c)(a, c) 减 2x2x,(d,e)(d, e) 加 2x2x。可以组合出 (a,b)(a, b) 加 2x2x,(a,c)(a, c) 减 2x2x。

到这里就会发现 n=4n = 4 是需要特判的。可以列出 66 个关于每个点作为 aa 的操作次数的方程,手动解一下方程即可。

现在 n≥5n \ge 5。发现如果所有边的边权都是偶数就做完了:从 nn 到 22 依次考虑,设当前点为 ii,可以把 (2,i),(3,i),…,(i−1,i)(2, i), (3, i), \ldots, (i - 1, i) 的边权全部清零,(1,i)(1, i) 暂时不管。最后把 (1,2),(1,3),…,(1,n)(1, 2), (1, 3), \ldots, (1, n) 的边权清零即可。这部分用的操作数是 4×(n2)≤2n24 \times \binom{n}{2} \le 2n^2。

所以现在我们的目标变成,让所有边的边权都变成偶数。

所以现在可以在模 22 意义下考虑:每条边的边权 ∈{0,1}\in \{0, 1\},操作变成选一个 K4K_4 子图,将其所有边的边权异或 11,目标仍然是清零所有边权。

然后乱手玩一些操作,应该可以发现:对于五个点 (a,b,c,d,e)(a, b, c, d, e),进行操作 (a,b,c,d),(a,b,c,e),(a,b,d,e),(a,c,d,e)(a, b, c, d), (a, b, c, e), (a, b, d, e), (a, c, d, e),效果是将 (a,b),(a,c),(a,d),(a,e)(a, b), (a, c), (a, d), (a, e) 的边权异或 11,其余边不受影响。

进一步地,若 n≥6n \ge 6,再找一个点 ff,先将 (a,b),(a,d),(a,e),(a,f)(a, b), (a, d), (a, e), (a, f) 异或 11,再将 (a,c),(a,d),(a,e),(a,f)(a, c), (a, d), (a, e), (a, f) 异或 11,效果就是将 (a,b),(a,c)(a, b), (a, c) 异或 11。

特别地,当 n=5n = 5 时,只有 55 种操作,枚举做哪些操作即可。

所以现在还是可以从 nn 到 22 依次考虑,设当前点为 ii,可以把 (2,i),(3,i),…,(i−1,i)(2, i), (3, i), \ldots, (i - 1, i) 的边权全部变成偶数,(1,i)(1, i) 暂时不管。最后再把 (1,2),(1,3),…,(1,n)(1, 2), (1, 3), \ldots, (1, n) 的边权变成偶数。这部分用的操作数是 8×(n2)≤4n28 \times \binom{n}{2} \le 4n^2。

最后总共操作不超过 2n2+4n2=6n22n^2 + 4n^2 = 6n^2 次,可以通过。

代码
C++
// Problem: P9841 [ICPC 2021 Nanjing R] Puzzle in Inazuma
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P9841
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 110;

ll n, a[maxn][maxn], tot;
bool vis[maxn];

struct node {
	ll a, b, c, d, x;
	node(ll _a = 0, ll _b = 0, ll _c = 0, ll _d = 0, ll _x = 0) : a(_a), b(_b), c(_c), d(_d), x(_x) {}
} ans[maxn * maxn * 10];

inline void oper(int i, int j, int k, int l, ll x) {
	if (!x) {
		return;
	}
	a[min(i, j)][max(i, j)] += x;
	a[min(i, k)][max(i, k)] += x;
	a[min(i, l)][max(i, l)] += x;
	a[min(j, k)][max(j, k)] -= x;
	a[min(j, l)][max(j, l)] -= x;
	a[min(k, l)][max(k, l)] -= x;
	ans[++tot] = node(i, j, k, l, x);
}

inline void oper2(int v, int w, int x, int y, int z) {
	oper(v, w, x, y, 1);
	oper(v, w, x, z, 1);
	oper(v, w, y, z, 1);
	oper(v, x, y, z, 1);
}

inline void oper3(int x, int y, int z) {
	vis[x] = vis[y] = vis[z] = 1;
	int p = 1;
	while (vis[p]) {
		++p;
	}
	vis[p] = 1;
	int q = p;
	while (vis[q]) {
		++q;
	}
	vis[q] = 1;
	int r = q;
	while (vis[r]) {
		++r;
	}
	vis[x] = vis[y] = vis[z] = vis[p] = vis[q] = 0;
	oper2(x, p, q, r, y);
	oper2(x, p, q, r, z);
}

inline void oper4(int i, int j, int k, int l, ll x) {
	oper(i, j, k, l, x >> 1);
	oper(j, i, k, l, x >> 1);
}

inline void oper5(int x, int y, int z, ll d) {
	vis[x] = vis[y] = vis[z] = 1;
	int p = 1;
	while (vis[p]) {
		++p;
	}
	vis[p] = 1;
	int q = p;
	while (vis[q]) {
		++q;
	}
	vis[x] = vis[y] = vis[z] = vis[p] = 0;
	oper4(x, y, p, q, d);
	oper4(x, z, p, q, -d);
}

struct wwh {
	int a, b, c, d;
	wwh(int _a = 0, int _b = 0, int _c = 0, int _d = 0) : a(_a), b(_b), c(_c), d(_d) {}
} c[99];

int b[9][9];

void solve() {
	scanf("%lld", &n);
	for (int i = 1; i <= n; ++i) {
		for (int j = i + 1; j <= n; ++j) {
			scanf("%lld", &a[i][j]);
		}
	}
	ll s = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = i + 1, x; j <= n; ++j) {
			scanf("%d", &x);
			a[i][j] -= x;
			s += a[i][j];
		}
	}
	if (s) {
		puts("-1");
		return;
	}
	if (n <= 4) {
		if (a[1][2] + a[3][4] || a[1][3] + a[2][4] || a[1][4] + a[2][3] || ((a[1][2] + a[1][3]) & 1) || ((a[1][2] + a[1][4]) & 1) || ((a[1][3] + a[1][4]) & 1)) {
			puts("-1");
			return;
		}
		printf("3\n2 1 3 4 %lld\n3 1 2 4 %lld\n4 1 2 3 %lld\n", (a[1][3] + a[1][4]) >> 1, (a[1][2] + a[1][4]) >> 1, (a[1][2] + a[1][3]) >> 1);
		return;
	}
	if (n >= 6) {
		for (int i = n; i; --i) {
			for (int j = i - 1; j >= 2; --j) {
				if (a[j][i] & 1) {
					oper3(i, j, j - 1);
				}
			}
		}
		for (int i = n; i >= 2; --i) {
			if (a[1][i] & 1) {
				oper3(1, i, i - 1);
			}
		}
	} else {
		int tt = 0;
		for (int i = 1; i <= n; ++i) {
			for (int j = i + 1; j <= n; ++j) {
				for (int k = j + 1; k <= n; ++k) {
					for (int l = k + 1; l <= n; ++l) {
						c[tt++] = wwh(i, j, k, l);
					}
				}
			}
		}
		bool f = 1;
		for (int S = 0; S < (1 << tt); ++S) {
			for (int i = 1; i <= n; ++i) {
				for (int j = i + 1; j <= n; ++j) {
					b[i][j] = (a[i][j] & 1);
				}
			}
			for (int i = 0; i < tt; ++i) {
				if (S & (1 << i)) {
					b[c[i].a][c[i].b] ^= 1;
					b[c[i].a][c[i].c] ^= 1;
					b[c[i].a][c[i].d] ^= 1;
					b[c[i].b][c[i].c] ^= 1;
					b[c[i].b][c[i].d] ^= 1;
					b[c[i].c][c[i].d] ^= 1;
				}
			}
			bool fl = 1;
			for (int i = 1; i <= n && fl; ++i) {
				for (int j = i + 1; j <= n && fl; ++j) {
					fl &= (!b[i][j]);
				}
			}
			if (fl) {
				f = 0;
				for (int i = 0; i < tt; ++i) {
					if (S & (1 << i)) {
						oper(c[i].a, c[i].b, c[i].c, c[i].d, 1);
					}
				}
				break;
			}
		}
		if (f) {
			puts("-1");
			return;
		}
	}
	for (int i = n; i; --i) {
		for (int j = i - 1; j >= 2; --j) {
			oper5(i, j, j - 1, -a[j][i]);
		}
	}
	for (int i = n; i >= 2; --i) {
		oper5(1, i, i - 1, -a[1][i]);
	}
	printf("%lld\n", tot);
	for (int i = 1; i <= tot; ++i) {
		printf("%lld %lld %lld %lld %lld\n", ans[i].a, ans[i].b, ans[i].c, ans[i].d, ans[i].x);
	}
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}