結果

問題 No.3338 Whole Reverse Contradiction
コンテスト
ユーザー ecottea
提出日時 2025-11-11 18:00:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 23,729 bytes
コンパイル時間 6,300 ms
コンパイル使用メモリ 310,312 KB
実行使用メモリ 8,100 KB
最終ジャッジ日時 2025-11-11 18:01:12
合計ジャッジ時間 15,336 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 66
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#ifndef HIDDEN_IN_VS // 折りたたみ用

// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS

// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;

// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9e18(int は -2^31 ~ 2^31 = 2e9)
using pii = pair<int, int>;	using pll = pair<ll, ll>;	using pil = pair<int, ll>;	using pli = pair<ll, int>;
using vi = vector<int>;		using vvi = vector<vi>;		using vvvi = vector<vvi>;	using vvvvi = vector<vvvi>;
using vl = vector<ll>;		using vvl = vector<vl>;		using vvvl = vector<vvl>;	using vvvvl = vector<vvvl>;
using vb = vector<bool>;	using vvb = vector<vb>;		using vvvb = vector<vvb>;
using vc = vector<char>;	using vvc = vector<vc>;		using vvvc = vector<vvc>;
using vd = vector<double>;	using vvd = vector<vd>;		using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;

// 定数の定義
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;

// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;

// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x)))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x)))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順)
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定

// 汎用関数の定義
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline int getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod

// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }

#endif // 折りたたみ用


#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;

#ifdef _MSC_VER
#include "localACL.hpp"
#endif

using mint = modint998244353;
//using mint = static_modint<(int)1e9+7>;
//using mint = modint; // mint::set_mod(m);

using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif


#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
int mute_dump = 0;
int frac_print = 0;
#if __has_include(<atcoder/all>)
namespace atcoder {
	inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
	inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
#endif
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_math(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE の代わりに MLE を出す
#endif


//【幅優先探索(動的)】O(n + m)(遅い)
/*
* st から到達可能な各頂点への最短距離を格納したリストを返す.
* nxt(s) は s の次に訪れることのできる頂点のリストを返す.
*/
template <class T, class FUNC>
map<T, int> dynamic_BFS(T st, const FUNC& nxt) {
	// verify : https://atcoder.jp/contests/abc241/tasks/abc241_f

	map<T, int> dist; // st からの最短距離を保持するテーブル
	dist[st] = 0;

	queue<T> que; // 次に探索する頂点を入れておくキュー
	que.push(st);

	while (!que.empty()) {
		// 未探索の頂点 s を得る.
		auto s = que.front(); que.pop();

		repe(t, nxt(s)) {
			// t が発見済みの頂点なら何もしない.
			if (dist.count(t)) continue;

			// スタートからの最短距離を確定する.
			dist[t] = dist[s] + 1;

			// 未探索の頂点として t を追加する.
			que.push(t);
		}
	}

	return dist;

	/* nxt の定義の雛形
	using T = ll;
	auto nxt = [&](T s) {
		vector<T> res;

		return res;
	};
	auto dist = dynamic_BFS(s, nxt);
	*/
}


void zikken() {
	int n = 4;

	using T = vvi;
	auto nxt = [&](T s) {
		vector<T> res;

		rep(x, n) rep(y, n) {
			auto t(s);
			rep(j, n) t[x][j] = s[x][n - 1 - j];

			auto t2(t);
			rep(i, n) t2[i][y] = t[n - 1 - i][y];

			res.push_back(t2);
		}

		return res;
	};

	vvi ini(n, vi(n));
	rep(i, n) rep(j, n) ini[i][j] = i * n + j;

	auto dist = dynamic_BFS(ini, nxt);

	for (auto [s, d] : dist) {
		int cnt = 0;
		rep(i, n) rep(j, n) {
			if (ini[i][j] != s[i][j]) {
				if ((i == 0 || i == n - 1) && (j == 0 || j == n - 1)) {
					cnt += 1;
				}
				else {
					cnt += 999;
				}
			}
		}

		if (cnt <= 3) {
			dump("----------------------------------");
			dump("cnt:", cnt, "dist:", d);
			dumpel(s);
		}
	}

	exit(0);
}
/*
----------------------------------
cnt: 0 dist: 0
 0: 0 1 2 3
 1: 4 5 6 7
 2: 8 9 10 11
 3: 12 13 14 15
----------------------------------
cnt: 3 dist: 4
 0: 0 1 2 12
 1: 4 5 6 7
 2: 8 9 10 11
 3: 15 13 14 3
----------------------------------
cnt: 3 dist: 2
 0: 0 1 2 15
 1: 4 5 6 7
 2: 8 9 10 11
 3: 3 13 14 12
----------------------------------
cnt: 3 dist: 2
 0: 3 1 2 12
 1: 4 5 6 7
 2: 8 9 10 11
 3: 0 13 14 15
----------------------------------
cnt: 3 dist: 4
 0: 3 1 2 15
 1: 4 5 6 7
 2: 8 9 10 11
 3: 12 13 14 0
----------------------------------
cnt: 3 dist: 4
 0: 12 1 2 0
 1: 4 5 6 7
 2: 8 9 10 11
 3: 3 13 14 15
----------------------------------
cnt: 3 dist: 2
 0: 12 1 2 3
 1: 4 5 6 7
 2: 8 9 10 11
 3: 15 13 14 0
----------------------------------
cnt: 3 dist: 2
 0: 15 1 2 0
 1: 4 5 6 7
 2: 8 9 10 11
 3: 12 13 14 3
----------------------------------
cnt: 3 dist: 4
 0: 15 1 2 3
 1: 4 5 6 7
 2: 8 9 10 11
 3: 0 13 14 12
*/


//【転倒数】O(n log n)
/*
* a[0..n) の転倒数を返す.
*/
template <class T>
ll inversion_number(const vector<T>& a) {
	// verify : https://atcoder.jp/contests/tessoku-book/tasks/tessoku_book_ef

	int n = sz(a);

	// 値 a[i] と位置 i を組にしソートする.
	vector<pair<T, int>> ai(n);
	rep(i, n) ai[i] = { a[i], i };
	sort(all(ai));

	ll res = 0;

	// ft[i] : いままでに位置 i の要素が現れたか
	fenwick_tree<int> ft(n);

	// 値について昇順に見ていく.
	rep(j, n) {
		// pos : 昇順で j 番目の値の位置
		int pos = ai[j].second;

		// pos より右に j 未満の要素が今までに何個あったかを加算する.
		res += ft.sum(pos + 1, n);

		// 位置 pos の要素の出現を記録する.
		ft.add(pos, 1);
	}

	return res;
}


//【ビット行列】
/*
* Bit_matrix<M>(int n, int m) : O(n M / 64)
*	n×m 零行列で初期化する.
*	制約:m ≦ M
*
* Bit_matrix<M>(int n) : O(n M / 64)
*	n×n 単位行列で初期化する.
*
* Bit_matrix<M>(vector<bitset<M>> a, int m) : O(n M / 64)
*	二次元配列 a[0..n)[0..m) の要素で初期化する.
*
* Bit_matrix<M>(vi[vl] a, int m) : O(n M / 64)
*	数 a[i] の第 j ビットを v[i][j] とする行列で初期化する.
*
* push_back(bitset<M> col) : O(M / 64)
*	最下行に col を追加する.
*
* A * x : O(n M / 64)
*	n×m 行列 A と m 次元列ベクトル x の積を返す.
*
* A * B : O(n m l / 64)
*	n×m 行列 A と m×l 行列 B の積を返す.
*
* Bit_matrix<M> pow(ll d) : O(n^3 log d / 64)
*	自身を d 乗した行列を返す.
*
* Bit_matrix<M> transpose() : O(n m)
*	自身を転置した行列を返す.
*	制約:n ≦ M
*/
template <int M>
struct Bit_matrix {
	int n, m; // 行列のサイズ(n 行 m 列)
	vector<bitset<M>> v; // 行列の成分

	// n×m 零行列で初期化する.
	Bit_matrix(int n, int m) : n(n), m(m), v(n) {}

	// n×n 単位行列で初期化する.
	Bit_matrix(int n) : n(n), m(n), v(n) { rep(i, n) v[i][i] = 1; }

	// 二次元配列 a[0..n)[0..m) の要素で初期化する.
	Bit_matrix(const vector<bitset<M>>& a, int m) : n(sz(a)), m(m), v(a) {}

	// 数 a[i] の第 j ビットを v[i][j] とする行列で初期化する.
	Bit_matrix(const vi& a, int m) : n(sz(a)), m(m), v(n) { rep(i, n) v[i] = bitset<M>(a[i]); }
	Bit_matrix(const vl& a, int m) : n(sz(a)), m(m), v(n) { rep(i, n) v[i] = bitset<M>(a[i]); }
	Bit_matrix() : m(0), n(0) {}

	// 代入
	Bit_matrix(const Bit_matrix& old) = default;
	Bit_matrix& operator=(const Bit_matrix& other) = default;

	// 比較
	bool operator==(const Bit_matrix& g) const { return n == g.n && m == g.m && v == g.v; }
	bool operator!=(const Bit_matrix& g) const { return !(*this == g); }

	// アクセス
	inline bitset<M> const& operator[](int i) const { return v[i]; }
	inline bitset<M>& operator[](int i) { return v[i]; }

	// 行の追加
	void push_back(const bitset<M>& col) {
		v.push_back(col);
		n++;
	}

	// 行列ベクトル積
	bitset<M> operator*(const bitset<M>& x) const {
		bitset<M> y;
		rep(i, n) y[i] = (v[i] & x).count() % 2;
		return y;
	}

	// 積
	Bit_matrix operator*(const Bit_matrix& b) const {
		// verify : https://judge.yosupo.jp/problem/matrix_product_mod_2

		vector<bitset<M>> bT(b.m);
		rep(i, b.n) rep(j, b.m) bT[j][i] = b[i][j];

		Bit_matrix res(n, b.m);
		rep(i, res.n) rep(j, res.m) res[i][j] = (v[i] & bT[j]).count() % 2;
		return res;
	}
	Bit_matrix& operator*=(const Bit_matrix& b) { *this = *this * b; return *this; }

	// 累乗
	Bit_matrix pow(ll d) const {
		// verify : https://atcoder.jp/contests/abc388/tasks/abc388_f

		Bit_matrix res(n), pow2 = *this;
		while (d > 0) {
			if (d & 1) res *= pow2;
			pow2 *= pow2;
			d /= 2;
		}
		return res;
	}

	// 転置(A^T)
	Bit_matrix transpose() const {
		Bit_matrix res(m, n);
		rep(i, m) rep(j, n) res[i][j] = v[j][i];
		return res;
	}

#ifdef _MSC_VER
	friend ostream& operator<<(ostream& os, const Bit_matrix& a) {
		rep(i, a.n) {
			os << "[";
			rep(j, a.m) os << a[i][j] << (j < a.m - 1 ? " " : "]");
			if (i < a.n - 1) os << "\n";
		}
		return os;
	}
#endif
};


//【線形方程式】O(n m min(n, m) / 64)
/*
* 与えられた n×m 行列 A と n 次元ベクトル b に対し,
* 線形方程式 A x = b の特殊解 x0(m 次元ベクトル)を格納する(なければ false を返す)
* また同次形 A x = 0 の解空間の基底(m 次元ベクトル)のリストを xs に格納する.
*/
template <int M>
bool gauss_jordan_elimination(const Bit_matrix<M>& A, const vb& b,
	bitset<M>* x0 = nullptr, vector<bitset<M>>* xs = nullptr)
{
	// verify : https://judge.yosupo.jp/problem/system_of_linear_equations_mod_2

	int n = A.n, m = A.m;

	// v : 拡大係数行列 (A | b)
	vector<bitset<M + 1>> v(n);
	rep(i, n) rep(j, m) v[i][j] = A[i][j];
	rep(i, n) v[i][m] = b[i];

	// pivots[i] : 第 i 行のピボットが第何列にあるか
	vi pivots;

	// 注目位置を v[i][j] とする.
	int i = 0, j = 0;

	while (i < n && j <= m) {
		// 注目列の下方の行から 1 を見つける.
		int i2 = i;
		while (i2 < n && !v[i2][j]) i2++;

		// 見つからなかったら注目位置を右に移す.
		if (i2 == n) { j++; continue; }

		// 見つかったら第 i 行とその行を入れ替える.
		if (i != i2) swap(v[i], v[i2]);

		// v[i][j] をピボットに選択する.
		pivots.push_back(j);

		// 第 i 行以外の第 j 列の成分が全て 0 になるよう第 i 行を XOR する.
		rep(i2, n) if (v[i2][j] && i2 != i) v[i2] ^= v[i];

		// 注目位置を右下に移す.
		i++; j++;
	}

	// 最後に見つかったピボットの位置が第 m 列ならば解なし.
	if (!pivots.empty() && pivots.back() == m) return false;

	// A x = b の特殊解 x0 の構成(任意定数は全て 0 にする)
	if (x0 != nullptr) {
		x0->reset();
		int rnk = sz(pivots);
		rep(i, rnk) (*x0)[pivots[i]] = v[i][m];

		// 同次形 A x = 0 の一般解 {x} の基底の構成(任意定数を 1-hot にする)
		if (xs != nullptr) {
			xs->clear();

			int i = 0;
			rep(j, m) {
				if (i < rnk && j == pivots[i]) {
					i++;
					continue;
				}

				bitset<M> x;
				x[j] = 1;
				rep(i2, i) x[pivots[i2]] = v[i2][j];
				xs->emplace_back(move(x));
			}
		}
	}

	return true;
}


vi solve(int n, vvi a) {
	if (a == vvi{ {1, 2}, {3, 0} }) {
		return vi{ 1, 0, 1, 1, 1 };
	}
	
	int hn = n / 2;

	Bit_matrix<300> mat(hn * hn, 2 * hn);
	vb vec(hn * hn);

	rep(i, hn) rep(j, hn) {
		vi seq(4);
		seq[0] = a[i][j];
		seq[1] = a[i][n - 1 - j];
		seq[2] = a[n - 1 - i][j];
		seq[3] = a[n - 1 - i][n - 1 - j];

		vi tgt(4);
		tgt[0] = i * n + j;
		tgt[1] = i * n + (n - 1 - j);
		tgt[2] = (n - 1 - i) * n + j;
		tgt[3] = (n - 1 - i) * n + (n - 1 - j);

		auto sseq(seq);
		uniq(sseq);
		if (tgt != sseq) return vi{-1};

		auto inv = inversion_number(seq);

		mat[i * hn + j][i] = 1;
		mat[i * hn + j][hn + j] = 1;
		vec[i * hn + j] = inv & 1;
	}
	dump("mat:"); dump(mat); dump("vec:"); dump(vec);

	bitset<300> ans;
	bool b = gauss_jordan_elimination<300>(mat, vec, &ans);
	if (!b) return vi{ -1 };
	
	vi xs, ys;
	rep(i, hn) if (ans[i]) xs.push_back(i);
	rep(j, hn) if (ans[hn + j]) ys.push_back(j);
	dump("xs:", xs); dump("ys:", ys);

	while (sz(ys) <= sz(xs) - 2) {
		ys.push_back(0);
		ys.push_back(0);
	}
	while (sz(xs) <= sz(ys) - 1) {
		xs.push_back(0);
		xs.push_back(0);
	}
	dump("xs:", xs); dump("ys:", ys);

	vi res;

	rep(i, sz(ys)) {
		res.push_back(xs[i]);
		rep(j, hn) swap(a[xs[i]][j], a[xs[i]][n - 1 - j]);

		res.push_back(ys[i]);
		rep(j, hn) swap(a[j][ys[i]], a[n - 1 - j][ys[i]]);
	}
	if (sz(xs) == sz(ys) + 1) {
		res.push_back(xs.back());
		rep(j, hn) swap(a[xs.back()][j], a[xs.back()][n - 1 - j]);
	}
	dump("a:"); dumpel(a); dump("res:", res);

	if (sz(res) % 2 == 0) {
		rep(i, hn) rep(j, hn) {
			dump("------- i:", i, "j:", j, "-------");

			// a[i][j] = i * n + j にする
			if (a[i][j] == i * n + j) {
				;
			}
			else if (a[i][n - 1 - j] == i * n + j) {
				res.push_back(i); swap(a[i][j], a[i][n - 1 - j]);
				res.push_back(j); swap(a[i][j], a[n - 1 - i][j]);
				res.push_back(i); swap(a[i][j], a[i][n - 1 - j]);
				res.push_back(j); swap(a[i][j], a[n - 1 - i][j]);
			}
			else if (a[n - 1 - i][j] == i * n + j) {
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
				res.push_back(j); swap(a[i][j], a[n - 1 - i][j]);
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
				res.push_back(j); swap(a[i][j], a[n - 1 - i][j]);
			}
			else {
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
				res.push_back(j); swap(a[i][j], a[n - 1 - i][j]);
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
				res.push_back(j); swap(a[i][j], a[n - 1 - i][j]);
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
				res.push_back(j); swap(a[i][j], a[n - 1 - i][j]);
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
				res.push_back(j); swap(a[i][j], a[n - 1 - i][j]);
			}
			dump("a:"); dumpel(a); dump("res:", res);

			// 全体を揃える
			if (a[n - 1 - i][j] == i * n + (n - 1 - j)) {
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
			}
			if (a[n - 1 - i][n - 1 - j] == i * n + (n - 1 - j)) {
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
			}
			dump("a:"); dumpel(a); dump("res:", res);
		}

		if (n & 1) {
			if (a[hn][0] > a[hn][n - 1]) {
				res.push_back(hn);
				rep(j, hn) swap(a[hn][j], a[hn][n - 1 - j]);

				if (a[0][hn] > a[n - 1][hn]) {
					res.push_back(hn);
					rep(i, hn) swap(a[i][hn], a[n - 1 - i][hn]);
				}
			}
			else {
				if (a[0][hn] > a[n - 1][hn]) {
					res.push_back(hn);
					rep(j, hn) swap(a[hn][j], a[hn][n - 1 - j]);

					res.push_back(hn);
					rep(i, hn) swap(a[i][hn], a[n - 1 - i][hn]);

					res.push_back(hn);
					rep(j, hn) swap(a[hn][j], a[hn][n - 1 - j]);
				}
			}
		}
	}
	else {
		rep(i, hn) rep(j, hn) {
			// a[i][j] = i * n + j にする
			if (a[i][j] == i * n + j) {
				;
			}
			else if (a[i][n - 1 - j] == i * n + j) {
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
				res.push_back(i); swap(a[i][j], a[i][n - 1 - j]);
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
				res.push_back(i); swap(a[i][j], a[i][n - 1 - j]);
			}
			else if (a[n - 1 - i][j] == i * n + j) {
				res.push_back(j); swap(a[i][j], a[n - 1 - i][j]);
				res.push_back(i); swap(a[i][j], a[i][n - 1 - j]);
				res.push_back(j); swap(a[i][j], a[n - 1 - i][j]);
				res.push_back(i); swap(a[i][j], a[i][n - 1 - j]);
			}
			else {
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
				res.push_back(i); swap(a[i][j], a[i][n - 1 - j]);
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
				res.push_back(i); swap(a[i][j], a[i][n - 1 - j]);
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
				res.push_back(i); swap(a[i][j], a[i][n - 1 - j]);
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
				res.push_back(i); swap(a[i][j], a[i][n - 1 - j]);
			}

			// 全体を揃える
			if (a[i][n - 1 - j] == (n - 1 - i) * n + j) {
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
			}
			if (a[n - 1 - i][n - 1 - j] == (n - 1 - i) * n + j) {
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
				res.push_back(n - 1 - j); swap(a[i][n - 1 - j], a[n - 1 - i][n - 1 - j]);
				res.push_back(n - 1 - i); swap(a[n - 1 - i][j], a[n - 1 - i][n - 1 - j]);
			}
		}

		if (n & 1) {
			if (a[0][hn] > a[n - 1][hn]) {
				res.push_back(hn);
				rep(i, hn) swap(a[i][hn], a[n - 1 - i][hn]);

				if (a[hn][0] > a[hn][n - 1]) {
					res.push_back(hn);
					rep(j, hn) swap(a[hn][j], a[hn][n - 1 - j]);
				}
			}
			else {
				if (a[hn][0] > a[hn][n - 1]) {
					res.push_back(hn);
					rep(i, hn) swap(a[i][hn], a[n - 1 - i][hn]);

					res.push_back(hn);
					rep(j, hn) swap(a[hn][j], a[hn][n - 1 - j]);

					res.push_back(hn);
					rep(i, hn) swap(a[i][hn], a[n - 1 - i][hn]);
				}
			}
		}
	}
	dump("a:"); dumpel(a); dump("res:", res);

	if (n & 1) {
		bool ok = true;
		rep(i, n) repi(j, hn, hn) if (a[i][j] != i * n + j) ok = false;
		repi(i, hn, hn) rep(j, n) if (a[i][j] != i * n + j) ok = false;
		if (!ok) return vi{ -1 };
	}

	return res;
}


pair<bool, vvi> check(int n, vvi a, vi res) {
	if (res == vi{ -1 }) return { true, vvi() };
	
	int K = sz(res);
	if (K > 4 * n * n) return { false, vvi() };

	rep(k, K) {
		if (k % 2 == 0) {
			int i = res[k];

			auto b(a);
			rep(j, n) b[i][j] = a[i][n - 1 - j];

			a = b;
		}
		else {
			int j = res[k];

			auto b(a);
			rep(i, n) b[i][j] = a[n - 1 - i][j];

			a = b;
		}
	}

	rep(i, n) rep(j, n) if (a[i][j] != i * n + j) {
		return { false, a };
	}

	return { true, vvi() };
}


void check2() {
	vi p{ 0,1,2,3 };

	repp(p) {
		int n = 2;

		vvi a(n, vi(n));
		rep(i, n) rep(j, n) a[i][j] = p[i * n + j];

		mute_dump = 1;
		auto res = solve(n, a);
		mute_dump = 0;

		auto [b, a2] = check(n, a, res);
		if (!b) {
			dump("------------- err --------------");
			dumpel(a);
			dump(res);
			dumpel(a2);
		}
	}

	exit(0);
}
/*
 0: 1 2
 1: 3 0
0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1
17
*/


void check3() {
	vi p{ 0,1,2,3,4,5,6,7,8 };

	repp(p) {
		int n = 3;

		vvi a(n, vi(n));
		rep(i, n) rep(j, n) a[i][j] = p[i * n + j];

		mute_dump = 1;
		auto res = solve(n, a);
		mute_dump = 0;

		auto [b, a2] = check(n, a, res);
		if (!b) {
			dump("------------- err --------------");
			dumpel(a);
			dump(res);
			dumpel(a2);
		}
	}

	exit(0);
}


void check4() {
	vi p{ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
	
	mt19937_64 mt((int)time(NULL));
	
	rep(hoge, 100000) {
		shuffle(all(p), mt);
		
		int n = 4;

		vvi a(n, vi(n));
		rep(i, n) rep(j, n) a[i][j] = p[i * n + j];

		mute_dump = 1;
		auto res = solve(n, a);
		mute_dump = 0;

		auto [b, a2] = check(n, a, res);
		if (!b) {
			dump("------------- err --------------");
			dumpel(a);
			dump(res);
			dumpel(a2);
		}
	}

	exit(0);
}
/*
------------- err --------------
 0: 8 5 2 4
 1: 14 0 15 12
 2: 7 3 9 1
 3: 6 13 11 10
0 0 0 0 2 0 2 0 2 0 2 0 2 3 2 3 2 2 2 2 2 2 2 2
 0: 4 5 2 6
 1: 1 0 3 7
 2: 12 9 15 14
 3: 8 13 11 10
*/


void Main() {
	int n;
	cin >> n;

	vvi a(n, vi(n));
	cin >> a;
	--a;

	auto res = solve(n, a);

	if (res == vi{ -1 }) {
		cout << -1 << "\n";
		return;
	}

	dump("ok?:", check(n, a, res));
	
	int K = sz(res);
	Assert(K <= 4 * n * n);

	cout << K << "\n";
	rep(k, K) cout << res[k] + 1 << " \n"[k == K - 1];
}

int main() {
	input_from_file("input.txt");
//	output_to_file("output.txt");

//	zikken();
//	check4();

	int t = 1;
	cin >> t; // マルチテストケースの場合

	while (t--) {
		dump("------------------------------");
		Main();
	}
}
0