// #include // Temp fix for gcc13 global pragma // #pragma GCC target("avx2,bmi2,popcnt,lzcnt") // #pragma GCC optimize("O3,unroll-loops") #include // #include using namespace std; #if __cplusplus >= 202002L using namespace numbers; #endif #ifdef LOCAL #include "Debug.h" #else #define debug_endl() 42 #define debug(...) 42 #define debug2(...) 42 #define debug_bin(...) 42 #endif #include #include using namespace __gnu_pbds; struct splitmix64_hash{ static unsigned long long _splitmix64(unsigned long long x){ x += 0x9e3779b97f4a7c15; x = (x ^ x >> 30) * 0xbf58476d1ce4e5b9; x = (x ^ x >> 27) * 0x94d049bb133111eb; return x ^ x >> 31; } size_t operator()(unsigned long long x) const{ static const unsigned long long FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return _splitmix64(x + FIXED_RANDOM); } template size_t operator()(const vector &a) const{ static const unsigned long long FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); unsigned long long h = 0; for(auto c: a) h = FIXED_RANDOM * h + c; return h; } }; template> using indexable_map = tree; template> using indexable_set = indexable_map; template using hash_map = __gnu_pbds::gp_hash_table; template using hash_set = hash_map; namespace direction_vectors{ vector> dr2{{1, 0}, {0, 1}}; vector> dr4{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; vector> dr4diag{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}}; vector> dr8{{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; vector> drk{{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}}; vector> generate(int low, int high){ assert(0 <= low && low <= high); int th = sqrt(high) + 1; vector> dr; for(auto x = -th; x <= th; ++ x) for(auto y = -th; y <= th; ++ y) if(auto d = x * x + y * y; low <= d && d <= high) dr.push_back({x, y}); return dr; } } int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); auto dr = direction_vectors::dr4; int n; cin >> n; vector coef(n, vector(n)); for(auto &x: coef | ranges::views::join){ cin >> x; } const int m = min(n, 4); vector a(m * m); for(auto i = 0; i < m; ++ i){ for(auto j = 0; j < m; ++ j){ a[m * i + j] = n * i + j; } } auto get_cost = [&](const vector &a)->long long{ long long cost = 0; for(auto i = 0; i < m; ++ i){ for(auto j = 0; j < m; ++ j){ cost += 1LL * coef[i][j] * a[m * i + j]; } } return cost; }; hash_set> found; found.insert(a); hash_map> mp; mp[get_cost(a)] = a; deque> dq{a}; while(!dq.empty()){ auto a = dq.front(); dq.pop_front(); int zi = -1, zj = -1; for(auto i = 0; i < m; ++ i){ for(auto j = 0; j < m; ++ j){ if(a[m * i + j] == 0){ zi = i, zj = j; goto DONE; } } } DONE:; for(auto [di, dj]: dr){ int in = zi + di, jn = zj + dj; if(0 <= min(in, jn) && max(in, jn) < m){ swap(a[m * zi + zj], a[m * in + jn]); if(found.insert(a).second){ auto ca = get_cost(a); if(!mp.insert({ca, a}).second){ auto b = mp[ca]; cout << "Yes\n"; for(auto rep = 2; rep; -- rep){ for(auto i = 0; i < n; ++ i){ for(auto j = 0; j < n; ++ j){ if(i < m && j < m){ cout << a[m * i + j] << " "; } else{ cout << n * i + j << " "; } } cout << "\n"; } swap(a, b); } return 0; } dq.push_back(a); } swap(a[m * zi + zj], a[m * in + jn]); } } } cout << "No\n"; return 0; } /* */