We are given n, m (both between 2 and 100). We have row xor values a_i (i=1..n) and column xor values b_j (j=1..m). We need to find a matrix X of size n x m with nonnegative integers (<= 2e9) that satisfies: For each row i, the xor of all elements in row i equals a_i. For each column j, xor of all elements in column j equals b_j.
We need to output "YES" and any such matrix, or "NO" if impossible.
This is a classic problem about constructing a matrix given row xors and column xors. We need to consider constraints: The xor of all elements in the matrix can be computed in two ways: by rows, the total xor (i.e., xor of all entries) equals xor_i a_i (since the xor of all entries equals the xor of the row xors? Wait careful: If we take xor over all entries, we can compute by first taking xor of each row (which gives a_i) and then xor those row results? Actually, xor is associative and commutative, and the xor of all entries = (xor over rows of (xor of row entries))? Not exactly: The xor over all entries equals the xor over i of (xor over j of x_{ij}). But the xor over a set of numbers can be partitioned arbitrarily, but the xor of a list of numbers is the same as the xor of groups of numbers then xor the results? Let's check: For numbers, xor is associative and commutative, and the xor of all elements is equal to the xor of the xors of any partition's groups? For example, consider numbers a, b, c, d. Compute xor of all = a xor b xor c xor d. Partition into groups {a,b} and {c,d}. Then xor of group1 = a xor b, group2 = c xor d. Then xor of group xors: (a xor b) xor (c xor d) = a xor b xor c xor d. So yes, because xor is associative and commutative and each element appears exactly once. So total xor = XOR_i (XOR_j x_{ij}) = XOR_i a_i. Similarly, total xor = XOR_j (XOR_i x_{ij}) = XOR_j b_j. So we must have XOR_i a_i = XOR_j b_j. This is a necessary condition. Is it also sufficient? Usually, we can construct a matrix. Known problem: given row xors and column xors, we can fill the matrix with zeros except some corner cell to satisfy constraints. But we also need to ensure non-negative integers (0 <= c_ij <= 2e9). Usually we can construct with small numbers.
Let's recall: We have n rows, m columns. Define variables x_{ij}. Constraints:
For each i: XOR_{j=1..m} x_{ij} = a_i.
For each j: XOR_{i=1..n} x_{ij} = b_j.
We need to find any solution. This is a system of linear equations over GF(2) if we consider each bit independently? Actually xor is bitwise independent. But the values are integers. However, we can treat each bit separately, but we can also combine. Since xor is linear over GF(2), we can think of each element as a vector of bits, but we can solve for the entire number using xor algebra.
One approach: Fill first (n-1) rows and (m-1) columns arbitrarily, and then determine the last row and last column to satisfy constraints, but the final corner cell must be consistent. That yields a condition.
Let me formalize: For i=1..n-1, j=1..m-1, choose any numbers (e.g., 0). Then compute for each i in 1..n-1, the value for last column m to satisfy row i: We have row i: x_{i1} xor ... xor x_{i,m-1} xor x_{im} = a_i => x_{im} = a_i xor (xor_{j=1}^{m-1} x_{ij}). So we can set x_{im} accordingly. Similarly, for each j in 1..m-1, to satisfy column j, we can compute x_{nj} = b_j xor (xor_{i=1}^{n-1} x_{ij}). Then we have the bottom right corner x_{nm} that appears in row n and column m. For row n: xor_{j=1}^{m} x_{nj} = a_n. But we already have x_{nj} for j=1..m-1 computed from column constraints, and x_{nm} is unknown. So we require:
(xor_{j=1}^{m-1} x_{nj}) xor x_{nm} = a_n => x_{nm} = a_n xor (xor_{j=1}^{m-1} x_{nj}).
But also from column m: xor_{i=1}^{n} x_{im} = b_m. We have x_{im} for i=1..n-1 computed from row constraints, and x_{nm} unknown. So requirement:
(xor_{i=1}^{n-1} x_{im}) xor x_{nm} = b_m => x_{nm} = b_m xor (xor_{i=1}^{n-1} x_{im}).
These two expressions for x_{nm} must be equal for consistency. So the condition is:
a_n xor (xor_{j=1}^{m-1} x_{nj}) = b_m xor (xor_{i=1}^{n-1} x_{im}).
We can substitute x_{nj} and x_{im} computed in terms of our chosen top-left (n-1)*(m-1) block and a_i, b_j. Let's compute.
Let’s denote chosen values for i=1..n-1, j=1..m-1 as c_{ij}. Then:
For i=1..n-1: x_{im} = a_i xor (xor_{j=1}^{m-1} c_{ij}). (1)
For j=1..m-1: x_{nj} = b_j xor (xor_{i=1}^{n-1} c_{ij}). (2)
Then:
Compute S = xor_{j=1}^{m-1} x_{nj} = xor_{j=1}^{m-1} [b_j xor (xor_{i=1}^{n-1} c_{ij})] = (xor_{j=1}^{m-1} b_j) xor (xor_{j=1}^{m-1} (xor_{i=1}^{n-1} c_{ij})).
Note that xor over j of xor over i of c_{ij} = xor_{i=1}^{n-1} xor_{j=1}^{m-1} c_{ij} (since xor is associative and commutative). Let's denote T = xor_{i=1}^{n-1} xor_{j=1}^{m-1} c_{ij}.
Then S = (xor_{j=1}^{m-1} b_j) xor T.
Similarly, compute R = xor_{i=1}^{n-1} x_{im} = xor_{i=1}^{n-1} [a_i xor (xor_{j=1}^{m-1} c_{ij})] = (xor_{i=1}^{n-1} a_i) xor (xor_{i=1}^{n-1} xor_{j=1}^{m-1} c_{ij}) = (xor_{i=1}^{n-1} a_i) xor T.
Now the condition for x_{nm} from row n: x_{nm} = a_n xor S = a_n xor (xor_{j=1}^{m-1} b_j) xor T.
From column m: x_{nm} = b_m xor R = b_m xor (xor_{i=1}^{n-1} a_i) xor T.
Equality implies:
a_n xor (xor_{j=1}^{m-1} b_j) xor T = b_m xor (xor_{i=1}^{n-1} a_i) xor T
Cancel T (xor both sides):
a_n xor (xor_{j=1}^{m-1} b_j) = b_m xor (xor_{i=1}^{n-1} a_i)
Rearranging: a_n xor (xor_{j=1}^{m-1} b_j) xor b_m xor (xor_{i=1}^{n-1} a_i) = 0
=> (xor_{i=1}^{n} a_i) xor (xor_{j=1}^{m} b_j) = 0.
Because xor_{i=1}^{n} a_i = (xor_{i=1}^{n-1} a_i) xor a_n, and xor_{j=1}^{m} b_j = (xor_{j=1}^{m-1} b_j) xor b_m. So condition becomes:
xor_i a_i = xor_j b_j.
So the necessary condition is that the xor of all row xors equals xor of all column xors. And that condition is also sufficient: we can set the top-left block arbitrarily (e.g., all zeros), then compute the rest accordingly. Let's test with zero block: c_{ij}=0 for all i<n, j<m.
Then:
For i=1..n-1: x_{im} = a_i xor (xor of zeros) = a_i.
For j=1..m-1: x_{nj} = b_j xor (xor of zeros) = b_j.
Now compute T = 0. Then S = xor_{j=1}^{m-1} x_{nj} = xor_{j=1}^{m-1} b_j. R = xor_{i=1}^{n-1} x_{im} = xor_{i=1}^{n-1} a_i.
Then x_{nm} from row n: a_n xor S = a_n xor (xor_{j=1}^{m-1} b_j). From column m: b_m xor R = b_m xor (xor_{i=1}^{n-1} a_i). Since xor_i a_i = xor_j b_j, these are equal. So x_{nm} = that value.
Thus we have a valid matrix. All entries are nonnegative integers. However, we need to ensure that all entries are within [0, 2e9]. Since a_i, b_j up to 1e9, the computed entries may be up to maybe 2e9? Let's see: a_i <= 1e9, b_j <= 1e9. x_{im}=a_i <= 1e9. x_{nj}=b_j <= 1e9. x_{nm} = a_n xor (xor_{j=1}^{m-1} b_j). Since each b_j <= 1e9, xor of up to 99 numbers each up to 1e9 can be as high as up to around 2^30 -1 (since 1e9 < 2^30, 1e9 ~ 1,073,741,824? Actually 2^30=1,073,741,824, so max value is < 2^30. XOR of numbers up to 2^30-1 is still within 2^30-1, i.e., < 1.074e9. a_n is also up to 1e9, so xor is within 2^30-1. So all numbers <= 1e9 roughly. That's within 2e9. So safe.
But is there any case where we get negative? No, all nonnegative.
Thus the condition is both necessary and sufficient. So algorithm: Check if xor of all a_i equals xor of all b_j. If not, output "NO". If yes, output "YES" and construct matrix as described:
Initialize matrix M with zeros (n x m). For i = 1..n-1: set M[i][m-1? Actually careful: we use 0-indexing. Let's use 0-based for implementation.
Let rows 0..n-1, cols 0..m-1.
Set:
- For i=0 to n-2 (i < n-1): M[i][m-1] = a[i] (since xor of row i's first (m-1) elements are 0 -> so M[i][m-1] = a[i]).
- For j=0 to m-2 (j < m-1): M[n-1][j] = b[j].
- Then compute corner: M[n-1][m-1] = a[n-1] xor (xor over j=0..m-2 of b[j])? Actually using row n-1: we have row n-1 elements: for j=0..m-2, M[n-1][j] = b[j]; unknown M[n-1][m-1] must satisfy row xor = a[n-1]. So M[n-1][m-1] = a[n-1] xor (xor_{j=0}^{m-2} b[j]). This should equal also b[m-1] xor (xor_{i=0}^{n-2} a[i]) due to condition, but we don't need to check.
Thus matrix is:
- Top-left (n-1)*(m-1) all 0.
- Rightmost column (except last row) = a[i] for i in 0..n-2.
- Bottom row (except last column) = b[j] for j in 0..m-2.
- Bottom-right corner = a[n-1] xor (xor of b[0..m-2]) (or equivalently b[m-1] xor (xor of a[0..n-2])).
Output all numbers.
But wait: Is the row xor condition satisfied for rows 0..n-2? Row i: all zeros in columns 0..m-2, and M[i][m-1] = a[i]. So xor of row = a[i] xor 0...0 = a[i]. Good.
For row n-1: the row consists of b[0], b[1], ..., b[m-2], and the corner value. xor of row = (xor_{j=0}^{m-2} b[j]) xor corner = (xor_{j=0}^{m-2} b[j]) xor (a[n-1] xor (xor_{j=0}^{m-2} b[j])) = a[n-1]. Good.
Columns: Column j for j=0..m-2: has zeros in rows 0..n-2, and in row n-1 = b[j]. So column xor = b[j] xor 0 = b[j]. Good.
Column m-1: has entries a[0], a[1], ..., a[n-2] in rows 0..n-2, and corner in row n-1. So xor of column = (xor_{i=0}^{n-2} a[i]) xor corner. Corner = a[n-1] xor (xor_{j=0}^{m-2} b[j]). Then column xor = (xor_{i=0}^{n-2} a[i]) xor a[n-1] xor (xor_{j=0}^{m-2} b[j]) = (xor_i a_i) xor (xor_{j=0}^{m-2} b[j]). But since condition says xor_i a_i = xor_j b_j = (xor_{j=0}^{m-2} b[j]) xor b[m-1], we have (xor_i a_i) xor (xor_{j=0}^{m-2} b[j]) = b[m-1]. So column m-1 xor = b[m-1], as needed. Good.
Thus construction works.
Edge Cases: n=2,m=2 works: top-left block is 1x1? Actually n-1=1, m-1=1. So we have M[0][0]=0 (top-left), M[0][1]=a[0], M[1][0]=b[0], M[1][1] = a[1] xor b[0] (or b[1] xor a[0]). Condition: a[0] xor a[1] = b[0] xor b[1]. That's necessary. Example: if a=[0,0], b=[0,0], matrix all zeros. Good.
What about n=2,m=3? That is first sample: n=2,m=3, a=[2,9], b=[5,3,13]. Check: xor a = 2 xor 9 = 11? 2 xor 9 = 11 (binary: 0010 xor 1001 = 1011 = 11). xor b = 5 xor 3 xor 13 = (5 xor 3=6) xor 13 = 6 xor 13 = 11? 6=0110,13=1101 -> 1011=11. Condition satisfied. Construct: n-1=1, m-1=2? Actually n=2 => n-1 =1 (top row index 0), m=3 => m-1=2 (columns 0..1 are first m-1=2). So top-left block is 1x2: set M[0][0]=0, M[0][1]=0? Wait careful: top-left block is rows 0..n-2 (i.e., row 0 only) and columns 0..m-2 (i.e., columns 0,1). So we set M[0][0]=0, M[0][1]=0. Then M[0][2] (rightmost column for top row) = a[0] = 2. Then M[1][0] = b[0]=5, M[1][1]=b[1]=3. Corner M[1][2] = a[1] xor (b[0] xor b[1]) = 9 xor (5 xor 3) = 9 xor 6 = 15? Wait 5 xor 3 = 6, 9 xor 6 = 15. That gives matrix:
[0, 0, 2]
[5, 3, 15]
But sample output shows:
3 4 5
6 7 8
Different but also valid. Our construction yields row0 xor: 0 xor 0 xor 2 =2; row1 xor:5 xor 3 xor 15= (5 xor 3=6) xor 15=9; column0:0 xor5=5; col1:0 xor3=3; col2:2 xor15=13. Works. So output would be YES with our matrix. So sample matches condition, but they output a different matrix. That's fine.
Second sample: n=3,m=3, a=[1,7,6] => xor = 1 xor 7 = 6, xor 6 = 0. b=[2,15,12] => 2 xor 15 = 13, xor 12 = 1? 13 xor 12 = 1? Actually 13 decimal (1101) xor 12 (1100) = 1 (0001). So xor a=0, xor b=1, not equal -> NO. So correct.
Thus solution is straightforward.
But wait: The problem constraints say n,m ≥ 2, but what about n=1 or m=1? They say 2 ≤ n, m ≤ 100. So both dimensions at least 2, so we always have at least one row and column for the top-left block? Actually n=2,m=2 yields n-1=1,m-1=1, top-left block is 1x1 (non-empty). Good. If n=2,m=100, top-left block is 1x99 (non-empty). So we can always use this method. For n=100,m=2, top-left block is 99x1. So works.
Potential pitfalls: In our construction, the top-left block is set to all zeros. That yields numbers that are exactly the given a_i and b_j etc. All numbers are nonnegative and ≤ something. But note the problem's output constraint: 0 ≤ c_{ij} ≤ 2·10^9. Since a_i, b_j ≤ 10^9, and XOR of some can be at most about 2^30-1, which is about 1e9, but could be up to (2^30-1) ~ 1.07e9, still ≤ 2e9. So fine.
Thus the solution is:
- Read n, m.
- Read array a of size n, array b of size m.
- Compute xor_a = XOR of all a_i; xor_b = XOR of all b_j.
- If xor_a != xor_b, print "NO".
- Else:
- Create matrix (list of lists) of size n x m initialized to 0.
- For i in range(n-1): matrix[i][m-1] = a[i] // last column except last row
- For j in range(m-1): matrix[n-1][j] = b[j] // last row except last column
- Compute corner = a[n-1] ^ (XOR of b[0..m-2]) // using xor over b[:-1]
- matrix[n-1][m-1] = corner
- Print "YES" and then matrix rows.
But careful: When n=2,m=2, the loops: for i in range(n-1) => i=0 => set M[0][1]=a[0]; for j in range(m-1) => j=0 => set M[1][0]=b[0]; corner computed = a[1] ^ (xor b[0..-1]? b[:-1] is b[0] only) => a[1] ^ b[0]; set M[1][1] = that. That works.
One nuance: In Python, we need to compute XOR of all a_i quickly.
Edge: The output might have numbers up to 2e9, but XOR of numbers can exceed 1e9? Since each a_i up to 10^9 ( < 2^30). XOR of many numbers each with up to 30 bits can produce a number with bits up to 2^30 -1. So max is less than 1,073,741,823, which is < 2e9. So fine.
Thus this is a simple solution.
But wait: The problem statement says "0 ≤ cij ≤ 2·10^9". Our numbers are within that. Also they could be up to 2e9. In case a_i, b_j are up to 1e9, XOR may be slightly larger than 1e9? Actually XOR of two numbers each less than 2^30 is less than 2^30, so yes within 1.07e9. So safe.
Thus implementation is straightforward.
But note: The problem might have multiple bits? Actually each a_i, b_j can be up to 10^9, but XOR operations are bitwise. So the solution stands.
Now let's consider if there is any other necessary condition? The xor condition seems both necessary and sufficient. Proof: Using the construction we gave.
Thus algorithm is O(n*m). n,m up to 100, trivial.
Now check sample 1: n=2,m=3, a=[2,9], b=[5,3,13]. XOR a=2 xor9=11, XOR b=5 xor3 xor13 = (5 xor3=6) xor13=6 xor13=11 -> equal. Construct matrix as above gave [[0,0,2],[5,3,15]]. That would be valid. But sample output gave [[3,4,5],[6,7,8]]. Row0 xor: 3 xor4 xor5 = (3 xor4=7)xor5=2? Actually 7 xor5=2, yes; row1:6 xor7 xor8 = (6 xor7=1)xor8=9; col0:3 xor6=5; col1:4 xor7=3; col2:5 xor8=13. So also valid. So any matrix works.
Thus answer: Check condition, if not, print NO; else print YES and the constructed matrix.
But we should verify for n=2,m=3 with our construction: matrix: row0:0 0 2; row1:5 3 15. Row0 xor: 2; row1 xor:5 xor3=6 xor15=9; col0:0 xor5=5; col1:0 xor3=3; col2:2 xor15=13. Works.
Potential issue: Some people might worry about the top-left block being zero, but the problem didn't restrict to positive, it's nonnegative. So zeros allowed.
Thus code.
But wait, there's a nuance: In our construction, the top-left block is of size (n-1)*(m-1). For n=2,m=2 it's 1x1, we set to 0. For n=100,m=2, it's 99x1, we set all those 99 cells to 0. That's fine.
But check column constraints for column 0 in such case: Column 0: for rows 0..n-2 (first 99 rows) are 0; row n-1 (last row) is b[0]. Column xor = b[0] -> correct. Row i for i=0..n-2: row has two columns: col0=0, col1 = a[i] (since last column index m-1 =1). Row xor = a[i] -> correct. Last row: col0 = b[0], col1 = corner computed = a[n-1] xor b[0]? Actually m-2 =0? If m=2, then m-2 =0? Let's compute: m=2, so m-1=1, there is only column index 0 as the first m-1 columns? Actually indices: columns 0 and 1. m-1 =1 is last column. So m-2 =0? That means we have one column in the "bottom row except last column", that is column 0. So set M[n-1][0] = b[0]. Then corner is a[n-1] xor (xor of b[0..m-2]) => b[0..m-2] is just b[0] (since m-2=0). So corner = a[n-1] xor b[0]. So last row: M[n-1][0] = b[0], M[n-1][1] = a[n-1] xor b[0]. Row xor = b[0] xor (a[n-1] xor b[0]) = a[n-1] -> correct. Column1: for rows i=0..n-2: M[i][1] = a[i]; row n-1: corner = a[n-1] xor b[0]; xor column1 = (xor_i=0..n-2 a[i]) xor (a[n-1] xor b[0]) = (xor_i a_i) xor b[0] =? Since xor_i a_i = xor_j b_j = b[0] xor b[1]? For m=2, b[1] is last column's b. Condition says xor_i a_i = b[0] xor b[1]. So column1 xor = (b[0] xor b[1]) xor b[0] = b[1] -> correct. So works.
Similarly for n=2,m=100.
Thus simple.
But wait: The problem constraints say 2 ≤ n, m ≤ 100, so both dimensions at least 2, so there is always at least one row and column to assign the "top-left" block? Actually if n=2,m=2, there is exactly one cell in top-left block, that's okay. So no zero-size issues.
Thus we can implement.
But I'd like to double-check the condition: xor_i a_i = xor_j b_j. That is necessary and sufficient.
Now consider if there might be any hidden trap: The values of a_i and b_j up to 1e9, but we need to output values up to 2e9. Our constructed numbers are either a_i, b_j, or xor of them, which will be at most (2^30 - 1) because a_i and b_j each < 2^30? Actually 1e9 ≈ 2^30 (1,073,741,824). If a_i can be exactly 1,073,741,823? But that's < 2^30. XOR of two such numbers could be up to 2^30 - 1, still < 2^30, which is ≈ 1.07e9, still less than 2e9. However, if a_i and b_j were up to 2e9, we could have XOR up to maybe 2^31 - 1. But constraints say up to 1e9. So safe.
Thus the solution is trivial.
But wait: In the problem statement, they say "0 ≤ cij ≤ 2·10^9". So we are safe.
Now, is there any chance that the constructed matrix would have values > 2e9? Let's compute max possible XOR: 1e9 is less than 2^30, but if a_i = 10^9 = 1000000000, in binary it's about 111011100110101100101000000000? Actually 10^9 decimal = 111011100110101100101000000000 (30 bits, 2^30=1073741824, 10^9 = 1000000000 < 1073741824). XOR of up to 100 numbers each with up to 30 bits could have bits up to 2^30 -1. So maximum is about 1073741823, which is < 2e9 (2,000,000,000). So safe.
Thus we can output that.
Now, is there any requirement that numbers be distinct? No.
Thus code is simple.
Now let's think if there is any alternative condition like if n,m > something? No.
I think we're done.
But let's consider if we should handle large values and maybe use Python's built-in XOR ^.
Implementation steps:
- read n,m as ints.
- read list a of n ints.
- read list b of m ints.
- compute xa = reduce(lambda x,y: x^y, a, 0) or simply use a loop.
- compute xb = reduce similarly.
- if xa != xb: print("NO")
- else:
- create matrix = [[0]*m for _ in range(n)]
- for i in range(n-1):
matrix[i][m-1] = a[i]
- for j in range(m-1):
matrix[n-1][j] = b[j]
- corner = a[n-1] ^ (xor of b[:-1]) # can compute as reduce xor of b[0:m-1]
b_xor_except_last = 0
for j in range(m-1):
b_xor_except_last ^= b[j]
corner = a[n-1] ^ b_xor_except_last
- matrix[n-1][m-1] = corner
- print("YES")
- for row in matrix: print(' '.join(map(str,row)))
But we should double-check the indexing: a[n-1] is the last element. Actually for row n-1 we used a[n-1] as row xor, and for the corner we used a[n-1] xor (xor of b[0..m-2]). That matches row n-1 equation.
Now check column m-1: we didn't explicitly set, but it's set via the assignments: column m-1 has a[0..n-2] and corner. As we argued, it will be correct due to condition.
Thus code works.
But we also need to ensure that the values are within limits. Yes.
Thus solution is complete.
Now, is there any possibility that the constructed matrix may contain numbers > 2e9? For example, if a_i and b_j are up to 10^9, XOR could be at most 2^30-1 which is about 1.07e9. However, if a_i and b_j were up to 2e9? But they are not. So safe.
Thus answer.
Now, I realize that the problem might be from Codeforces (CF 1016B? Actually there is a problem: "Vasya and the Matrix" maybe). The solution is exactly this.
Now, we can produce final answer.
But we must be careful: In our construction, we set the top-left block to zeros. However, in some cases, the zeros might cause the matrix to have all entries within limit, but is there any possibility that the corner becomes negative? No, xor yields nonnegative integer.
One more check: For n=2,m=3 with a=[2,9], b=[5,3,13], our corner computed as 9 ^ (5^3) = 9 ^ 6 = 15. That's <= 2e9.
All good.
Now, what about when n=2,m=2 and a=[0,0], b=[0,0] -> matrix all zeros.
If a=[1,1], b=[1,1] -> xor condition: 1 xor 1 =0, b: 1 xor 1 =0 -> good. matrix: top-left 0, top-right a0=1, bottom-left b0=1, corner = a1 xor b0 =1 xor 1=0 -> matrix [[0,1],[1,0]]. Row0 xor: 0 xor1=1, row1:1 xor0=1, col0:0 xor1=1, col1:1 xor0=1. Works.
Now, test a case where condition fails: e.g., a=[0,1], b=[0,0] -> xor a=1, xor b=0 -> NO.
Thus correct.
Now, we need to output matrix if exists.
Now, note: There is a nuance: The problem says "0 ≤ cij ≤ 2·10^9". In our construction, we have zeros in the top-left block. That's okay.
Now, is it possible that using zeros leads to a value that is exactly > 2e9? As argued, no.
Thus final.
Now I'll produce final solution explanation and code.
But the problem is straightforward. However, there is another nuance: Sometimes one might need to check if the condition holds for all bits separately? Actually XOR condition is global, and it's necessary and sufficient. So fine.
Thus answer.
Now, let's write final solution in Python.
Pseudo:
def solve():
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
xa = 0
for v in a: xa ^= v
xb = 0
for v in b: xb ^= v
if xa != xb:
print("NO")
return
mat = [[0]*m for _ in range(n)]
# fill last column for first n-1 rows
for i in range(n-1):
mat[i][m-1] = a[i]
# fill last row for first m-1 columns
for j in range(m-1):
mat[n-1][j] = b[j]
# compute corner
bxor = 0
for j in range(m-1):
bxor ^= b[j]
mat[n-1][m-1] = a[n-1] ^ bxor
print("YES")
for row in mat:
print(' '.join(map(str,row)))
That's it.
But I recall that there is also a solution where we can set all cells to zero except first row and first column, etc. This is similar.
Now, ensure that we computed bxor correctly: XOR of b[0] to b[m-2] inclusive.
Now, what about if n=100,m=100, we have many zeros; that's fine.
Time complexity O(n*m) which is 10^4 operations, trivial.
Now, we might want to use list comprehension for printing.
Now, double-check for case m=1? Not allowed, because m ≥2. So we don't need to worry about m-1 being zero? Actually m=2, m-1=1, m-2=0, our loop for j in range(m-1) will be range(1) => j=0, correct. The bxor loop will be range(m-1) => range(1) -> j=0, compute bxor = b[0]. Good.
What about n=2, m=2? range(n-1)=range(1) => i=0, set mat[0][1]=a[0]. range(m-1)=range(1) => j=0, set mat[1][0]=b[0]. bxor = b[0] from range(1). corner = a[1] ^ b[0]. Works.
Now, we might also consider using sys.stdout write for speed, but not needed.
Thus final answer.
Now, let's produce final output with explanation.
But the user wants "final" answer. They likely expect a solution explanation and code. However the problem statement is typical of a Codeforces problem. The user said "Now Vasya is taking an exam..." So they probably want solution code.
Thus I'll provide a clear explanation and Python code.
Wait: In the problem statement, there is a constraint: "0 ≤ cij ≤ 2·10^9". Our constructed numbers are less than that, but if a_i or b_j are 10^9, and we XOR them maybe produce number > 10^9? Still less than 2e9, as argued. But let's confirm: 10^9 = 1000000000; XOR of two such numbers could be as high as (2^30-1)=1073741823. That's still less than 2e9. So fine.
But what about XOR of many numbers? The max possible bit-length is max bits among them. So max stays within 2^30-1. So okay.
Thus final.
Now, I'll output final answer.
But also note that there is a potential alternative: if we set the top-left block to something else, we might get different matrix; but we choose zeros for simplicity.
Now I'm going to provide final answer: explanation and code.
Now the final. The solution relies on the necessary and sufficient condition that the XOR of all row xors equals the XOR of all column xors. If this condition holds, a matrix can be constructed by:
- Setting all cells in the first
n-1 rows and first m-1 columns to 0.
- For each row
i (0‑based, i < n-1), set the last column element to a[i].
- For each column
j (0‑based, j < m-1), set the last row element to b[j].
- Set the bottom‑right corner to
a[n-1] ^ (b[0] ^ b[1] ^ … ^ b[m-2]).
This yields a matrix satisfying all constraints.
If the XOR condition is not met, no such matrix exists.
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = int(next(it))
a = [int(next(it)) for _ in range(n)]
b = [int(next(it)) for _ in range(m)]
xor_a = 0
for v in a:
xor_a ^= v
xor_b = 0
for v in b:
xor_b ^= v
if xor_a != xor_b:
print("NO")
return
# Create matrix filled with zeros
mat = [[0] * m for _ in range(n)]
# Fill last column of first n-1 rows
for i in range(n - 1):
mat[i][m - 1] = a[i]
# Fill last row of first m-1 columns
for j in range(m - 1):
mat[n - 1][j] = b[j]
# Compute XOR of b[0..m-2]
b_xor_except_last = 0
for j in range(m - 1):
b_xor_except_last ^= b[j]
# Bottom‑right corner
mat[n - 1][m - 1] = a[n - 1] ^ b_xor_except_last
print("YES")
out_lines = []
for row in mat:
out_lines.append(' '.join(map(str, row)))
sys.stdout.write('\n'.join(out_lines))
if __name__ == "__main__":
solve()