Submission details
Task:Ruudukko
Sender:hltk
Submission time:2025-09-28 19:39:59 +0300
Language:C++ (C++20)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED100
Test results
testverdicttime
#1ACCEPTED0.01 sdetails

Code

#include <iostream>
#include <queue>

using namespace std;

void solve(int n, int m) {
	bool t = 0;
	if (m < n) {
		swap(n, m);
		t = 1;
	}
	if (m == 2 || (n == 1 && m == 3)) {
		cout << "NO" << endl;
		return;
	}
	cout << "YES" << endl;
	if (m == 1) {
		cout << 1 << endl;
	} else if (n == 2 && m == 3) {
		if (t) {
			cout << 1 << " " << 4 << endl;
			cout << 3 << " " << 6 << endl;
			cout << 5 << " " << 2 << endl;
		} else {
			cout << 1 << " " << 3 << " " << 5 << endl;
			cout << 4 << " " << 6 << " " << 2 << endl;
		}
	} else if (m == 3) {
		cout << 1 << " " << 4 << " " << 7 << endl;
		cout << 8 << " " << 2 << " " << 5 << endl;
		cout << 3 << " " << 6 << " " << 9 << endl;
	} else {
		vector<int> v(m);
		for (int i = 0; i < m; ++i) {
			v[i] = i % 2 ? i / 2 : i / 2 + m / 2;
		}
		if (m == 4) {
			v = {1, 3, 0, 2};
		}
		if (t) {
			for (int j = 0; j < m; ++j) {
				for (int i = 0; i < n; ++i) {
					cout << v[j] + m * i + 1 << " ";
				}
				cout << endl;
			}
		} else {
			for (int i = 0; i < n; ++i) {
				for (int j = 0; j < m; ++j) {
					cout << v[j] + m * i + 1 << " ";
				}
				cout << endl;
			}
		}
	}
}

int main() {
	int t;
	cin >> t;

	while (t--) {
		int n, m;
		cin >> n >> m;

		solve(n, m);
	}
}

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
...