#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; void answer(int n, const VI& p, const VI& q) { cout << "Yes\n"; rep(i, n) rep(j, n) cout << p[i * n + j] << " \n"[j + 1 == n]; rep(i, n) rep(j, n) cout << q[i * n + j] << " \n"[j + 1 == n]; } std::default_random_engine gen(std::chrono::system_clock::now().time_since_epoch().count()); using dist_type = std::uniform_int_distribution<>; int RI(int L, int R) { return dist_type(L, R - 1)(gen); } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int n2 = n * n; VI a(n2); rep(i, n2) cin >> a[i]; VI x(n2); iota(all(x), 0); if (n <= 3) { map memo; do { ll v = 0; rep(i, n2) v += (ll)a[i] * x[i]; auto [it, inserted] = memo.emplace(v, x); if (!inserted) { answer(n, it->second, x); return 0; } } while (next_permutation(all(x))); cout << "No\n"; return 0; } unordered_map memo; vector

hist; ll now = 0; rep(i, n2) now += (ll)a[i] * x[i]; memo[now] = ssize(hist); while (true) { int p1 = RI(0, n2), p2 = RI(0, n2); if (p1 == p2) continue; int dx = x[p2] - x[p1]; now += (ll)a[p1] * dx - (ll)a[p2] * dx; swap(x[p1], x[p2]); hist.emplace_back(p1, p2); auto [it, inserted] = memo.emplace(now, ssize(hist)); if (!inserted) { VI x1 = x; while (ssize(hist) != it->second) { auto [p1, p2] = hist.back(); hist.pop_back(); swap(x[p1], x[p2]); } answer(n, x, x1); return 0; } } }