#P8167. [eJOI 2021] XCopy

[eJOI 2021] XCopy

Problem Description

Given positive integers N,MN, M.

Please construct an N×MN \times M matrix containing only non-negative integers, such that all elements are pairwise distinct, and any two adjacent (up, down, left, right) elements differ in exactly one bit in binary. Also, make the maximum element in the matrix as small as possible.

Input Format

One line with two positive integers N,MN, M.

Output Format

Output an N×MN \times M matrix containing only non-negative integers. The Special Judge is very strict about the output format: there must be no extra characters such as spaces at the end of each line.

3 3
5 4 6
1 0 2
9 8 10

Hint

Scoring Rules

This problem uses partial scoring. The score depends on the size of your output answer compared with the correct answer. Here, “answer” means the maximum element in the matrix.

Let SS be the full score of this test point, and OO be the correct answer. If GG is your output answer, then the score is:

$$S \times \max(1-\sqrt{\dfrac{\frac{G}{O}-1}{3}},0)$$

Because of Luogu’s scoring mechanism, the actual score for each test point is the floor of the above value, i.e. $\lfloor S \times \max(1-\sqrt{\dfrac{\frac{G}{O}-1}{3}},0) \rfloor$. For example, when S=7,G=15,O=10S=7, G=15, O=10, the score by the formula is about 4.144.14, but on Luogu the score is 44.

If the output matrix does not meet the requirements (i.e. all elements are distinct and any two adjacent elements differ by exactly one bit in binary), then this test point gets 00 points.

Note that, since the Special Judge has strict formatting requirements, please do not add extra spaces at the end of each line when printing your answer, otherwise the corresponding test point will be scored as 00. Also, please do not output integers outside the range [0,263)[0,2^{63}) (long long), otherwise it will be judged as a format error and scored as 00.

Sample Explanation

The sample matrix is:

01012=5100101_2=5_{10} 01002=4100100_2=4_{10} 01102=6100110_2=6_{10}
00012=1100001_2=1_{10} 00002=0100000_2=0_{10} 00102=2100010_2=2_{10}
10012=9101001_2=9_{10} 10002=8101000_2=8_{10} 10102=10101010_2=10_{10}

It is easy to see that every pair of adjacent elements differs by exactly one bit in binary. The answer of this construction is 1010 (i.e. the maximum element), and it is the smallest among all valid constructions. Of course, other matrices with maximum element 1010 are also valid (for example, by flipping the matrix, etc.).

A construction that can get partial points is:

011020110_2 011120111_2 010120101_2
111021110_2 111121111_2 110121101_2
101021010_2 101121011_2 100121001_2

The maximum element of this matrix is 1515. According to the scoring rules, before flooring it can get $\max(1-\sqrt{\dfrac{\frac{15}{10}-1}{3}},0)=1-\dfrac{\sqrt{6}}{6} \approx 59\%$ of the score for this test point.

Constraints and Notes

This problem uses bundled testdata.

  • Subtask 1 (7 pts): N=1N=1.
  • Subtask 2 (9 pts): N,MN, M are integer powers of 22.
  • Subtask 3 (14 pts): NN is an integer power of 22.
  • Subtask 4 (70 pts): no special restrictions.

For 100%100\% of the data, 1N,M20001 \le N, M \le 2000.

Notes

This problem is translated from eJOI2021 Day 1 C XCopy. You are welcome to hack the self-written Special Judge via private messages or forum posts.

Translated by ChatGPT 5