This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 200005;
int t;
int n, m;
ll k;
vi g[MAXN];
namespace st4 {
int main() {
ll mn = n / 2, mx = (ll) (n / 2) * (n / 2);
if (k < mn || k > mx) {
cout << "NO\n";
return 0;
}
if (k == mn + 1 || k == mx - 1) {
cout << "NO\n";
return 0;
}
cout << "YES\n";
REP (i, 1, n + 1) {
g[i] = vi(m + 1);
}
int rt = -1;
RREP (i, n / 2, 1) {
if ((ll) i * i + n / 2 - i <= k) {
rt = i;
break;
}
}
assert(rt != -1);
int ptr = 1;
if ((ll) (rt + 1) * (rt + 1) + n / 2 - rt - 2 == k) {
RREP (k, n / 2, rt + 2) {
for (int i : {ptr, n - ptr + 1}) {
REP (j, ptr, m - ptr + 2) {
g[i][j] = ptr;
}
}
REP (i, ptr, n - ptr + 2) {
for (int j : {ptr, m - ptr + 1}) {
g[i][j] = ptr;
}
}
ptr++;
}
REP (i, ptr, n - ptr + 2) {
g[i][ptr + 1] = ptr - 1;
}
int optr = ptr;
for (int i = optr - 1; i <= n - optr + 1; i += 2) {
REP (a, 0, 2) {
REP (b, 0, 2) {
g[i + a][optr - 1 + b] = ptr;
}
}
ptr++;
}
for (int i = optr; i <= n - optr + 1; i += 2) {
for (int j = optr + 2; j <= m - optr + 1; j += 2) {
if (i - 4 < optr && j + 4 > m - optr + 1) {
continue;
}
REP (a, 0, 2) {
REP (b, 0, 2) {
g[i + a][j + b] = ptr;
}
}
ptr++;
}
}
REP (i, optr, optr + 4) {
RREP (j, m - optr + 1, m - optr - 2) {
g[i][j] = ptr;
}
}
ptr++;
REP (i, optr + 1, optr + 3) {
RREP (j, m - optr, m - optr - 1) {
g[i][j] = ptr;
}
}
ptr++;
} else {
ll need = k - ((ll) rt * rt + n / 2 - rt);
RREP (k, n / 2, rt + 1) {
for (int i : {ptr, n - ptr + 1}) {
REP (j, ptr, m - ptr + 2) {
g[i][j] = ptr;
}
}
REP (i, ptr, n - ptr + 2) {
for (int j : {ptr, m - ptr + 1}) {
g[i][j] = ptr;
}
}
ptr++;
}
int optr = ptr;
int lft = m - optr - 1;
if (need < rt) {
lft = optr + need * 2 - 1;
}
for (int i = optr - 1; i <= n - optr + 2; i += 2) {
for (int j = optr - 1; j < lft; j += 2) {
REP (a, 0, 2) {
REP (b, 0, 2) {
g[i + a][j + b] = ptr;
}
}
ptr++;
}
}
REP (i, optr, n - optr + 2) {
g[i][lft] = optr - 1;
}
need = max(0ll, need - rt + 1);
cerr << optr << '\n';
for (int i = n - optr + 1; i > n - optr + 1 - 2 * need; i -= 2) {
for (int j = lft; j <= n - optr + 1; j += 2) {
REP (a, 0, 2) {
REP (b, 0, 2) {
g[i + a][j + b] = ptr;
}
}
ptr++;
}
REP (j, lft, n - optr + 3) {
g[i - 1][j] = optr - 1;
}
}
for (int i = optr; i <= n - optr + 1 - 2 * need; i += 2) {
for (int j = lft + 1; j <= n - optr; j += 2) {
REP (a, 0, 2) {
REP (b, 0, 2) {
g[i + a][j + b] = ptr;
}
}
ptr++;
}
}
}
REP (i, 1, n + 1) {
REP (j, 1, n + 1) {
cout << setfill(' ') << setw(2) << g[i][j] << ' ';
}
cout << '\n';
}
assert(ptr - 1 == k);
return 0;
}
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> t;
while (t--) {
cin >> n >> m >> k;
if ((n & 1) || (m & 1)) {
cout << "NO\n";
continue;
}
if (n == 2 && m <= 4) {
if (k != m / 2) {
cout << "NO\n";
} else {
cout << "YES\n";
REP (i, 0, n) {
REP (j, 0, m) {
cout << j / 2 + 1 << ' ';
}
cout << '\n';
}
}
continue;
}
if (m == 2 && n <= 4) {
if (k != n / 2) {
cout << "NO\n";
} else {
cout << "YES\n";
REP (i, 0, n) {
REP (j, 0, m) {
cout << i / 2 + 1 << ' ';
}
cout << '\n';
}
}
continue;
}
if (n == m) {
st4::main();
continue;
}
ll mx = (ll) n / 2 * m / 2, mn = max(n, m) / 2;
if (k < mn || k > mx) {
cout << "NO\n";
continue;
}
if (k == mx - 1) {
cout << "NO\n";
continue;
}
cout << "YES\n";
bool swp = 0;
if (n > m) {
swap(n, m);
swp = 1;
}
REP (i, 1, n + 1) {
g[i] = vi(m + 1);
}
int rt = -1;
RREP (i, n / 2, 1) {
if ((ll) i * (m / 2 - n / 2 + i) + n / 2 - i <= k) {
rt = i;
break;
}
}
assert(rt != -1);
int ptr = 1;
if ((ll) (rt + 1) * (m / 2 - n / 2 + rt + 1) + n / 2 - rt - 2 == k) {
RREP (k, n / 2, rt + 2) {
for (int i : {ptr, n - ptr + 1}) {
REP (j, ptr, m - ptr + 2) {
g[i][j] = ptr;
}
}
REP (i, ptr, n - ptr + 2) {
for (int j : {ptr, m - ptr + 1}) {
g[i][j] = ptr;
}
}
ptr++;
}
REP (i, ptr, n - ptr + 2) {
g[i][ptr + 1] = ptr - 1;
}
int optr = ptr;
for (int i = optr - 1; i <= n - optr + 1; i += 2) {
REP (a, 0, 2) {
REP (b, 0, 2) {
g[i + a][optr - 1 + b] = ptr;
}
}
ptr++;
}
for (int i = optr; i <= n - optr + 1; i += 2) {
for (int j = optr + 2; j <= m - optr + 1; j += 2) {
if (i - 4 < optr && j + 4 > m - optr + 1) {
continue;
}
REP (a, 0, 2) {
REP (b, 0, 2) {
g[i + a][j + b] = ptr;
}
}
ptr++;
}
}
REP (i, optr, optr + 4) {
RREP (j, m - optr + 1, m - optr - 2) {
g[i][j] = ptr;
}
}
ptr++;
REP (i, optr + 1, optr + 3) {
RREP (j, m - optr, m - optr - 1) {
g[i][j] = ptr;
}
}
ptr++;
} else {
ll need = k - ((ll) rt * (m / 2 - n / 2 + rt) + n / 2 - rt);
cerr << ' ' << need << ' ' << rt << '\n';
RREP (k, n / 2, rt + 1) {
for (int i : {ptr, n - ptr + 1}) {
REP (j, ptr, m - ptr + 2) {
g[i][j] = ptr;
}
}
REP (i, ptr, n - ptr + 2) {
for (int j : {ptr, m - ptr + 1}) {
g[i][j] = ptr;
}
}
ptr++;
}
int optr = ptr;
int lft = min((ll) m - optr - 1, optr + need * 2 - 1);
cerr << lft << '\n';
for (int i = optr - 1; i <= n - optr + 2; i += 2) {
for (int j = optr - 1; j < lft; j += 2) {
REP (a, 0, 2) {
REP (b, 0, 2) {
g[i + a][j + b] = ptr;
}
}
ptr++;
}
}
REP (i, optr, n - optr + 2) {
g[i][lft] = optr - 1;
}
need -= min(need, (ll) (lft - optr + 1) / 2);
cerr << optr << ' ' << need << '\n';
for (int i = n - optr + 1; i > n - optr + 1 - 2 * need; i -= 2) {
for (int j = lft; j <= m - optr + 1; j += 2) {
REP (a, 0, 2) {
REP (b, 0, 2) {
g[i + a][j + b] = ptr;
}
}
ptr++;
}
REP (j, lft, m - optr + 3) {
g[i - 1][j] = optr - 1;
}
}
for (int i = optr; i <= n - optr + 1 - 2 * need; i += 2) {
for (int j = lft + 1; j <= m - optr; j += 2) {
REP (a, 0, 2) {
REP (b, 0, 2) {
g[i + a][j + b] = ptr;
}
}
ptr++;
}
}
}
if (swp) {
REP (i, 1, m + 1) {
REP (j, 1, n + 1) {
cout << setfill(' ') << setw(2) << g[j][i] << ' ';
}
cout << '\n';
}
} else {
REP (i, 1, n + 1) {
REP (j, 1, m + 1) {
cout << setfill(' ') << setw(2) << g[i][j] << ' ';
}
cout << '\n';
}
}
assert(ptr - 1 == k);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |