Submission details
Task:Ruudukko
Sender:cb14641
Submission time:2025-09-27 17:53:12 +0300
Language:C++ (C++20)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED100
Test results
testverdicttime
#1ACCEPTED0.00 sdetails

Code

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for (int i = a; i < b; i++)

vi line(int n) {
    vi A(n);
    if (n == 4)
        A = {2, 4, 1, 3};
    if (n == 5)
        A = {1, 3, 5, 2, 4};
    if (n == 6)
        A = {1, 3, 5, 2, 4, 6};
    if (n == 7)
        A = {1, 3, 5, 7, 2, 4, 6};
    if (n == 8)
        A = {1, 3, 5, 7, 2, 4, 6, 8};
    if (n == 9)
        A = {1, 3, 5, 7, 9, 2, 4, 6, 8};
    if (n == 10)
        A = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
    return A;
}

void test() {
    int N, M;
    cin >> M >> N;

    if ((1 < N * M && N * M <= 3) || (N == 2 && M == 2)) {
        cout << "NO\n";
        return;
    }

    bool flipped = false;
    if (N < M) {
        swap(N, M);
        flipped = true;
    }

    vvi box(M, vi(N));

    if (N == 1) {
        box[0][0] = 1;
    } else if (N == 2 || (N == 3 && M == 1)) {
        cout << "NO\n";
        return;
    } else if (N == 3) {
        if (M == 2)
            box = {{1, 5, 3}, {4, 2, 6}};
        if (M == 3)
            box = {{1, 6, 2}, {7, 3, 8}, {4, 9, 5}};
    } else if (N >= 4) {
        box[0] = line(N);
        for (int m = 1; m < M; m++)
            for (int n = 0; n < N; n++)
                box[m][n] = box[m - 1][n] + N;
    }

    vvi trans(N, vi(M));
    for (int m = 0; m < M; m++)
        for (int n = 0; n < N; n++)
            trans[n][m] = box[m][n];
    if (flipped) {
        swap(M, N);
        box = trans;
    }

    cout << "YES\n";
    for (int m = 0; m < M; m++) {
        for (int n = 0; n < N; n++) {
            cout << box[m][n];
            if (n < N - 1)
                cout << " ";
        }
        cout << "\n";
    }
}

int main() {
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);

    int T;
    cin >> T;
    for (int t = 0; t < T; t++)
        test();
}

Test details

Test 1

Verdict: ACCEPTED

input
100
1 1
1 2
2 1
1 3
...

correct output
YES
1
NO
NO
NO
...

user output
YES
1
NO
NO
NO
...