# EXBANNER2 - Editorial

**URL:** <https://discuss.codechef.com/t/exbanner2-editorial/126061>\
**Category:** editorial\
**Tags:** editorial, start261\
**Created:** [October 21, 2026, 4:30pm UTC](https://discuss.codechef.com/t/exbanner2-editorial/126061 "2026-10-21T16:30:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceknight1093](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@iceknight1093](https://discuss.codechef.com/u/iceknight1093)\
**Post date:** [October 21, 2026, 4:30pm UTC](https://discuss.codechef.com/t/exbanner2-editorial/126061/1 "2026-10-21T16:30:53Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/EXBANNER2)  
[Contest: Division 1](https://www.codechef.com/START261A/problems/EXBANNER2)  
[Contest: Division 2](https://www.codechef.com/START261B/problems/EXBANNER2)  
[Contest: Division 3](https://www.codechef.com/START261C/problems/EXBANNER2)  
[Contest: Division 4](https://www.codechef.com/START261D/problems/EXBANNER2)

_ **Author:** _ [am\_aadvik](https://www.codechef.com/users/am_aadvik)  
_ **Tester:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)  
_ **Editorialist:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)

# DIFFICULTY:

TBD

# PREREQUISITES:

None

# PROBLEM:

You are given the row-wise and column-wise bitwise ANDs, ORs, and XORs of an N\times M grid.  
Reconstruct any grid that satisfies these conditions, if one exists.

Additional constraints: at least 4 rows and columns each, every bitwise AND is 0, every bitwise OR is 1, every bitwise XOR is in \{0, 1\}.

# EXPLANATION:

The additional given constraints are clearly important, so let’s see what they mean.

Every bitwise OR is 1, so every value in the grid must be either 0 or 1.  
Further, every row/column OR being 1 means that:

- Every row must contain a 1, and
- Every column must contain a 1.

Similarly, every row/column AND being 0 means that:

- Every row must contain a 0, and every column must contain a 0.  
(This is because each value is either 0 or 1.)

With these two handled, ANDs and ORs are completely taken care of; so we only need to worry about XORs.

Let R\_i denote the XOR of the i-th row, and C\_i the XOR of the i-th column.

Note that:

- R\_1 \oplus R\_2\oplus\ldots\oplus R\_N equals the XOR of the whole grid.
- C\_1 \oplus C\_2\oplus\ldots\oplus C\_M _also_ equals the XOR of the whole grid.
- Thus, these two values must be equal; otherwise no solution exists.

It turns out that as long as the row/column XORs match, a solution always exists.  
Let’s now move to constructing a solution.

* * *

Let’s first try solving for a 4\times 4 grid, since that’s the minimal case.

We start by enforcing the constraint that every row and every column contains both a 0 and 1.  
This can be done using exactly two elements in each row/column - for example by placing the ones and zeros across one diagonal each.  
For example, you could do

\begin{bmatrix} 1 & 0 & a & b \\ c & 1 & 0 & d \\ e & f & 1 & 0 \\ 0 & g & h & 1 \end{bmatrix}

where a, b, \ldots, h denote undecided variables.

Let’s now look at the undecided values.  
Suppose we fix the value of a, say to 0.  
Then,

- b is uniquely determined by the first row’s XOR, to be R\_1\oplus 1
- Then, d is uniquely determined by the last column’s XOR, and must equal C\_4\oplus 1\oplus b
- Fixing d then fixes c by the second row’s XOR.
- Fixing c fixes e, which then fixes f, which then fixes g.

Finally, we are left with h, which you may notice is uniquely determined by the XOR of _both_ its row and its column.  
However, since we are in a situation where the XOR of row XORs equals the XOR of column XORs, both its row and its column will provide h with the same value - so there are no compatibility issues!

This way, we have completely solved a 4\times 4 grid, as long as the row and column XORs are compatible.

* * *

We now extend this idea to a general N\times M grid, where N, M \ge 4.  
(Of course, the first check is still that row and column XORs are compatible - we continue from that.)

First, we’ll handle rows and columns beyond 4.

- For each 5 \le i \le N, do the following in the i-th row:
  - Place a 0 in the first column.
  - Place a 1 in the second column.
  - If R\_i = 0, also place a 1 in the third column; so that the XOR condition is satisfied.

- For each 5 \le i \le M, do something similar for the i-th column: place a 0 and 1 in the first row, and then use the third row to satisfy the XOR condition if needed.

Observe that this completely handles all rows and columns starting from 5.  
This means we only need to worry about rows 1 to 4, i.e. a 4\times 4 grid.  
We already know how to solve this!

Note that our initial placements for satisfying the larger rows/columns might have affected the expected row/column XORs for the top left 4\times 4 grid, so make sure to modify them appropriately based on the already placed values.  
However, since the row and column XORs are compatible a solution will exist nevertheless.

# TIME COMPLEXITY:

\mathcal{O}(NM) per testcase.

# CODE:

> **Editorialist's code (PyPy3)**
>
> ```python
> for _ in range(int(input())):
> n, m = map(int, input().split())
>     
> rx, cx = [], []
> tot = 0
> for i in range(n):
> x, o, a = map(int, input().split())
> rx.append(x)
> tot ^= x
> for i in range(m):
> x, o, a = map(int, input().split())
> cx.append(x)
> tot ^= x
> if tot == 1:
> print(-1)
> continue
>     
> ans = [[0]*m for _ in range(n) ]
>     
> for i in range(4, n):
> ans[i][1] = 1
> ans[i][2] = 1 ^ rx[i]
>         
> cx[1] ^= 1
> cx[2] ^= ans[i][2]
> for i in range(4, m):
> ans[1][i] = 1
> ans[2][i] = 1 ^ cx[i]
>         
> rx[1] ^= 1
> rx[2] ^= ans[2][i]
>         
> for i in range(4):
> ans[i][i] = 1
>     
> ans[0][3] = 1 ^ rx[0]
> ans[1][3] = 1 ^ cx[3] ^ ans[0][3]
> ans[1][0] = 1 ^ rx[1] ^ ans[1][3]
> ans[2][0] = 1 ^ cx[0] ^ ans[1][0]
> ans[2][1] = 1 ^ rx[2] ^ ans[2][0]
> ans[3][1] = 1 ^ cx[1] ^ ans[2][1]
> ans[3][2] = 1 ^ rx[3] ^ ans[3][1]
>     
> for row in ans: print(*row)
> 
> ```
