luogu#P9382. [THUPC 2023 决赛] Freshman Dream

[THUPC 2023 决赛] Freshman Dream

Problem Description

Little J is learning matrix multiplication.

Little L tells him: as long as you multiply the elements in the same positions of two matrices, you can get the product of the two matrices.

This is of course wrong, but Little L wants to keep fooling Little J. So, she needs to put a matrix multiplication problem on her own OJ such that this kind of “matrix multiplication” can also produce the correct answer.

Because Little L’s OJ runs very slowly and has a very small memory limit, the answers in this matrix multiplication problem are all taken modulo 22.

Now Little L starts generating testdata. She first randomly generates an n×nn \times n matrix AA. Specifically, each element is 11 with probability 12\frac 12, and 00 otherwise, and these events are independent. Now, she still needs to design another n×nn \times n 0101 matrix BB, such that (AB)ijAijBij(mod2)(AB)_{ij} \equiv A_{ij} B_{ij} \pmod 2.

Little L tried generating matrices randomly, but could not find any matrix that meets the requirement; she tried constructing some matrices and found she could only construct the all-zero matrix, which is too obvious. Now she hands the task of generating testdata to you: you need to give a BB that meets the requirement. At the same time, to avoid making it look suspicious, she additionally requires that BB contains exactly kk ones.

Input Format

Read from standard input.

The first line contains two positive integers n,kn, k, representing the matrix size and the kk in the statement.

The next nn lines each contain nn integers aija_{ij}, representing the elements of AA.

Output Format

Write to standard output.

If there is no BB that satisfies the requirement, output one line containing one integer 1-1.

Otherwise, first output one line containing one integer 11, then output nn lines, each containing nn integers in {0,1}\{0,1\} to represent the elements of matrix BB. If there are multiple possible BB, output any one of them.

3 3
1 0 0
0 1 0
0 0 1
1
1 0 0
0 1 0
0 0 1

Hint

Sample Explanation #1

Here AA is the identity matrix, and the constructed BB is also the identity matrix, so the product is also the identity matrix. At the same time, multiplying the corresponding positions also gives the identity matrix, and BB contains exactly k=3k = 3 ones, so it satisfies the requirement.

In this sample, nn is not 100100, but it is guaranteed that in all testdata nn is 100100.

Constraints

For all testdata, n=100n = 100, 0kn20 \le k \le n^2, aij{0,1}a_{ij} \in \{0,1\}, and all aija_{ij} are independent and uniformly random.

Source

From the finals of the 2023 Tsinghua University Student Programming Contest and Collegiate Invitational (THUPC2023).

Resources such as the editorial can be found at https://github.com/THUSAAC/THUPC2023.

Translated by ChatGPT 5