[Math] form matrix using xor of rows and xor of columns

discrete mathematics

Given :

Matrix consist of $n$ rows and m columns

For each row the xor of all elements in that row (suppose if there are 5 rows in a matrix then 5 numbers will be given representing the xor of each row)

For each column the xor of all element in that column

Task:

If it is possible to form a matrix then print the elements of that matrix

I found this in the editorial

If $a_1\oplus a_2 \oplus\cdots\oplus a_n\neq b_1 \oplus b2 \oplus\cdots\oplus b_m$ , then there is no suitable matrix. The operation means xor.

Otherwise, we can always construct a suitable matrix by the following method: the first element of the first line will be equal to `$a_1 \oplus b_2 \oplus b_3 \oplus\cdots\oplus b_m$. The second element of the first line is $b_2$, the third element is $b_3$, the last one is $b_m$.

The first element of the second line will be $a_2$, the first element of the third line is $a_3$, the first element of the last line is an. The rest of the elements will be zero.

It is not difficult to verify that the matrix obtained satisfies all the restrictions.

I tried reading https://codeforces.com/contest/1016/problem/D , but I didn't understand how the elements of matrix are calculated. Please can someone explain it with example.

Best Answer

The point is that xor is a commutative and associative operation, so the xor of the xors of the rows must be the same as the xor of the xors of the columns.

For example if the matrix is

$$\pmatrix{1 & 2 & 3\cr 4 & 5 & 6\cr}$$

we have $$1 \oplus 2 \oplus 3 \oplus 4 \oplus 5 \oplus 6 = (1 \oplus 2 \oplus 3) \oplus (4 \oplus 5 \oplus 6) = (1 \oplus 4) \oplus (2 \oplus 5) \oplus (3 \oplus 6)$$

EDIT: Let's say you want a $2 \times 3$ matrix $A$ where the xor's of the rows are $a = [4, 1]$ and the xor's of the columns are $b = [2, 3, 4]$. This does satisfy the condition. We take the first element of the first row to be $a_1 \oplus b_2 \oplus b_3 = 4 \oplus 3 \oplus 4 = 3$, the others $b_2 =3$ and $b_3 = 4$, the first element of the second row to be $a_2 = 1$, and all other elements $0$. Thus the matrix is $$ \pmatrix{3 & 3 & 4\cr 1 & 0 & 0\cr} $$

Related Question