Problem:


Score : 600 points

Problem Statement

Given are an integer N and arrays S, T, U, and V, each of length N.
Construct an N×N matrix a that satisfy the following conditions:

a_{i,j} is an integer.

0 \leq a_{i,j}  \lt 2^{64}.

If S_{i} = 0, the bitwise AND of the elements in the i-th row is U_{i}.

If S_{i} = 1, the bitwise OR of the elements in the i-th row is U_{i}.

If T_{i} = 0, the bitwise AND of the elements in the i-th column is V_{i}.

If T_{i} = 1, the bitwise OR of the elements in the i-th column is V_{i}.

However, there may be cases where no matrix satisfies the conditions.

Constraints

All values in input are integers.

 1 \leq N \leq 500 

 0 \leq S_{i} \leq 1 

 0 \leq T_{i} \leq 1  

 0 \leq U_{i} \lt 2^{64}  

 0 \leq V_{i} \lt 2^{64}  

Input

Input is given from Standard Input in the following format:

N
S_{1} S_{2} ...  S_{N}
T_{1} T_{2} ...  T_{N}
U_{1} U_{2} ...  U_{N}
V_{1} V_{2} ...  V_{N}

Output

If there exists a matrix that satisfies the conditions, print one such matrix in the following format:

a_{1,1} ...  a_{1,N}
:
a_{N,1} ...  a_{N,N}

Note that any matrix satisfying the conditions is accepted.

If no matrix satisfies the conditions, print -1.

Sample Input 1

2
0 1
1 0
1 1
1 0

Sample Output 1

1 1
1 0

In Sample Input 1, we need to find a matrix such that:

the bitwise AND of the elements in the 1-st row is 1;

the bitwise OR of the elements in the 2-nd row is 1;

the bitwise OR of the elements in the 1-st column is 1;

the bitwise AND of the elements in the 2-nd column is 0.

Sample Input 2

2
1 1
1 0
15 15
15 11

Sample Output 2

15 11
15 11



Sample input:
2
0 1
1 0
1 1
1 0


Sample output:
1 1
1 0

Submission status: Wrong Answer

Solution code:
import numpy as np

def solve_1bit(N, rps, cps, rv, cv):
    res = np.full((N, N), 2, dtype=np.uint64)
    if N == 1:
        res[0, 0] = rv[0]
        return res
    r_all = [False, False]
    for i, (ps, v) in enumerate(zip(rps, rv)):
        if ps ^ v == 1:
            res[i, :] = v
            r_all[v] = True
    c_all = [False, False]
    for i, (ps, v) in enumerate(zip(cps, cv)):
        if ps ^ v == 1:
            res[:, i] = v
            c_all[v] = True
    if (r_all[0] and c_all[1]) or (r_all[1] and c_all[0]):
        return res
    if r_all[0] and c_all[0]:
        return np.where(res==2, 1, res)
    if r_all[1] and c_all[1]:
        return np.where(res==2, 0, res)
    if all(r_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = cv[x]
        return res
    if all(c_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = rv[y]
        return res
    if r_all[0]:
        if (cv==0).any():
            x = (cv==0).nonzero()[0][0]
            res[:, x] = 0
            return np.where(res==2, 1, res)
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 0 and res[y, 0] == 2:
                res[y, x] = 0
                x = (x+1) % N
        return np.where(res==2, 1, res)
    if r_all[1]:
        if (cv==1).any():
            x = (cv==1).nonzero()[0][0]
            res[:, x] = 1
            return np.where(res==2, 0, res)
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 1 and res[y, 0] == 2:
                res[y, x] = 1
                x = (x+1) % N
        return np.where(res==2, 0, res)
    if c_all[0]:
        if (rv==0).any():
            y = (rv==0).nonzero()[0][0]
            res[y, :] = 0
            return np.where(res==2, 1, res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 0 and res[0, x] == 2:
                res[y, x] = 0
                y = (y+1) % N
        return np.where(res==2, 1, res)
    if c_all[1]:
        if (rv==1).any():
            y = (rv==1).nonzero()[0][0]
            res[y, :] = 1
            return np.where(res==2, 0, res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 1 and res[0, x] == 2:
                res[y, x] = 1
                y = (y+1) % N
        return np.where(res==2, 0, res)
    for y in range(N):
        for x in range(N):
            res[y, x] = (y^x)&1
    return res

def solve(N, row_prod_sum, col_prod_sum, row_val, col_val):
    res = np.zeros((N, N), dtype=np.uint64)
    for i in range(64):
        rps = row_prod_sum
        cps = col_prod_sum
        rv = row_val>>i&1
        cv = col_val>>i&1
        r = solve_1bit(N, rps, cps, rv, cv)
        res |= r<<i
    #print(res)
    prod = res[0].copy()
    sum_ = res[0].copy()
    for y in range(1, N):
        prod &= res[y]
        sum_ |= res[y]
    #print(prod, sum_)
    if (prod[col_prod_sum==0] != col_val[col_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[col_prod_sum==1] != col_val[col_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    prod = res[:, 0].copy()
    sum_ = res[:, 0].copy()
    for x in range(1, N):
        prod &= res[:, x]
        sum_ |= res[:, x]
    #print(prod, sum_)
    if (prod[row_prod_sum==0] != row_val[row_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[row_prod_sum==1] != row_val[row_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    return 1, res



N = int(input())
row_prod_sum = np.array(input().split(), dtype=np.uint64)
col_prod_sum = np.array(input().split(), dtype=np.uint64)
row_val = np.array(input().split(), dtype=np.uint64)
col_val = np.array(input().split(), dtype=np.uint64)
t, ans = solve(N, row_prod_sum, col_prod_sum, row_val, col_val)
if t:
    for lst in ans.tolist():
        print(" ".join(map(str, lst)))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import numpy as np


def solve_1bit(N, rps, cps, rv, cv):
    res = np.full((N, N), 2, dtype=np.uint64)
    if N == 1:
        res[0, 0] = rv[0]
        return res
    r_all = [False, False]
    for i, (ps, v) in enumerate(zip(rps, rv)):
        if ps ^ v == 1:
            res[i, :] = v
            r_all[v] = True
    c_all = [False, False]
    for i, (ps, v) in enumerate(zip(cps, cv)):
        if ps ^ v == 1:
            res[:, i] = v
            c_all[v] = True
    if (r_all[0] and c_all[1]) or (r_all[1] and c_all[0]):
        return res
    if r_all[0] and c_all[0]:
        return np.where(res==2, 1, res)
    if r_all[1] and c_all[1]:
        return np.where(res==2, 0, res)
    if all(r_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = cv[x]
        return res
    if all(c_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = rv[y]
        return res
    if r_all[0]:
        if (cv==0).any():
            x = (cv==0).nonzero()[0][0]
            res[:, x] = 0
            return np.where(res==2, 1, res)
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 0 and res[y, 0] == 2:
                res[y, x] = 0
                x = (x+1) % N
        return np.where(res==2, 1, res)
    if r_all[1]:
        if (cv==1).any():
            x = (cv==1).nonzero()[0][0]
            res[:, x] = 1
            return np.where(res==2, 0, res)
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 1 and res[y, 0] == 2:
                res[y, x] = 1
                x = (x+1) % N
        return np.where(res==2, 0, res)
    if c_all[0]:
        if (rv==0).any():
            y = (rv==0).nonzero()[0][0]
            res[y, :] = 0
            return np.where(res==2, 1, res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 0 and res[0, x] == 2:
                res[y, x] = 0
                y = (y+1) % N
        return np.where(res==2, 1, res)
    if c_all[1]:
        if (rv==1).any():
            y = (rv==1).nonzero()[0][0]
            res[y, :] = 1
            return np.where(res==2, 0, res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 1 and res[0, x] == 2:
                res[y, x] = 1
                y = (y+1) % N
        return np.where(res==2, 0, res)
    for y in range(N):
        for x in range(N):
            res[y, x] = (y^x)&1
    return res

def solve(N, row_prod_sum, col_prod_sum, row_val, col_val):
    res = np.zeros((N, N), dtype=np.uint64)
    for i in range(64):
        rps = row_prod_sum
        cps = col_prod_sum
        rv = row_val>>i&1
        cv = col_val>>i&1
        r = solve_1bit(N, rps, cps, rv, cv)
        res |= r<<i
    return 1, res
    #print(res)
    prod = res[0].copy()
    sum_ = res[0].copy()
    for y in range(1, N):
        prod &= res[y]
        sum_ |= res[y]
    #print(prod, sum_)
    if (prod[col_prod_sum==0] != col_val[col_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[col_prod_sum==1] != col_val[col_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    prod = res[:, 0].copy()
    sum_ = res[:, 0].copy()
    for x in range(1, N):
        prod &= res[:, x]
        sum_ |= res[:, x]
    #print(prod, sum_)
    if (prod[row_prod_sum==0] != row_val[row_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[row_prod_sum==1] != row_val[row_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    return 1, res



N = int(input())
row_prod_sum = np.array(input().split(), dtype=np.uint64)
col_prod_sum = np.array(input().split(), dtype=np.uint64)
row_val = np.array(input().split(), dtype=np.uint64)
col_val = np.array(input().split(), dtype=np.uint64)
t, ans = solve(N, row_prod_sum, col_prod_sum, row_val, col_val)
if t:
    for lst in ans.tolist():
        print(" ".join(map(str, lst)))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import numpy as np



def solve_1bit(N, rps, cps, rv, cv):
    res = np.full((N, N), 2, dtype=np.uint64)
    if N == 1:
        res[0, 0] = rv[0]
        return res
    r_all = [False, False]
    for i, (ps, v) in enumerate(zip(rps, rv)):
        if ps ^ v == 1:
            res[i, :] = v
            r_all[v] = True
    c_all = [False, False]
    for i, (ps, v) in enumerate(zip(cps, cv)):
        if ps ^ v == 1:
            res[:, i] = v
            c_all[v] = True
    if (r_all[0] and c_all[1]) or (r_all[1] and c_all[0]):
        return res
    if r_all[0] and c_all[0]:
        return np.where(res==2, 1, res)
    if r_all[1] and c_all[1]:
        return np.where(res==2, 0, res)
    if all(r_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = cv[x]
        return res
    if all(c_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = rv[y]
        return res
    if r_all[0]:
        if (cv==0).any():
            x = (cv==0).nonzero()[0][0]
            res[:, x] = 0
            return np.where(res==2, 1, res)
        return res
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 0 and rv[y, 0] == 2:
                res[y, x] = 0
                x = (x+1) % N
        return np.where(res==2, 1, res)
    return res
    if r_all[1]:
        if (cv==1).any():
            x = (cv==1).nonzero()[0][0]
            res[:, x] = 1
            return np.where(res==2, 0, res)
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 1 and rv[y, 0] == 2:
                res[y, x] = 1
                x = (x+1) % N
        return np.where(res==2, 0, res)
    if c_all[0]:
        if (rv==0).any():
            y = (rv==0).nonzero()[0][0]
            res[y, :] = 0
            return np.where(res==2, 1, res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 0 and cv[0, x] == 2:
                res[y, x] = 0
                y = (y+1) % N
        return np.where(res==2, 1, res)
    if c_all[1]:
        if (rv==1).any():
            y = (rv==1).nonzero()[0][0]
            res[y, :] = 1
            return np.where(res==2, 0, res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 1 and cv[0, x] == 2:
                res[y, x] = 1
                y = (y+1) % N
        return np.where(res==2, 0, res)
    for y in range(N):
        for x in range(N):
            res[y, x] = (y^x)&1
    return res

def solve(N, row_prod_sum, col_prod_sum, row_val, col_val):
    res = np.zeros((N, N), dtype=np.uint64)
    for i in range(64):
        rps = row_prod_sum
        cps = col_prod_sum
        rv = row_val>>i&1
        cv = col_val>>i&1
        r = solve_1bit(N, rps, cps, rv, cv)
        res |= r<<i
    return 1, res
    #print(res)
    prod = res[0].copy()
    sum_ = res[0].copy()
    for y in range(1, N):
        prod &= res[y]
        sum_ |= res[y]
    #print(prod, sum_)
    if (prod[col_prod_sum==0] != col_val[col_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[col_prod_sum==1] != col_val[col_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    prod = res[:, 0].copy()
    sum_ = res[:, 0].copy()
    for x in range(1, N):
        prod &= res[:, x]
        sum_ |= res[:, x]
    #print(prod, sum_)
    if (prod[row_prod_sum==0] != row_val[row_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[row_prod_sum==1] != row_val[row_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    return 1, res



N = int(input())
row_prod_sum = np.array(input().split(), dtype=np.uint64)
col_prod_sum = np.array(input().split(), dtype=np.uint64)
row_val = np.array(input().split(), dtype=np.uint64)
col_val = np.array(input().split(), dtype=np.uint64)
t, ans = solve(N, row_prod_sum, col_prod_sum, row_val, col_val)
if t:
    for lst in ans.tolist():
        print(" ".join(map(str, lst)))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import numpy as np

def solve_1bit(N, rps, cps, rv, cv):
    res = np.full((N, N), 2, dtype=np.uint64)
    if N == 1:
        res[0, 0] = rv[0]
        return res
    r_all = [False, False]
    for i, (ps, v) in enumerate(zip(rps, rv)):
        if ps ^ v == 1:
            res[i, :] = v
            r_all[v] = True
    c_all = [False, False]
    for i, (ps, v) in enumerate(zip(cps, cv)):
        if ps ^ v == 1:
            res[:, i] = v
            c_all[v] = True
    if (r_all[0] and c_all[1]) or (r_all[1] and c_all[0]):
        return res
    if r_all[0] and c_all[0]:
        return np.where(res==2, 1, res)
    if r_all[1] and c_all[1]:
        return np.where(res==2, 0, res)
    if all(r_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = cv[x]
        return res
    if all(c_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = rv[y]
        return res
    return res
    if r_all[0]:
        if (cv==0).any():
            x = (cv==0).nonzero()[0][0]
            res[:, x] = 0
            return np.where(res==2, 1, res)
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 0 and rv[y, 0] == 2:
                res[y, x] = 0
                x = (x+1) % N
        return np.where(res==2, 1, res)
    if r_all[1]:
        if (cv==1).any():
            x = (cv==1).nonzero()[0][0]
            res[:, x] = 1
            return np.where(res==2, 0, res)
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 1 and rv[y, 0] == 2:
                res[y, x] = 1
                x = (x+1) % N
        return np.where(res==2, 0, res)
    if c_all[0]:
        if (rv==0).any():
            y = (rv==0).nonzero()[0][0]
            res[y, :] = 0
            return np.where(res==2, 1, res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 0 and cv[0, x] == 2:
                res[y, x] = 0
                y = (y+1) % N
        return np.where(res==2, 1, res)
    if c_all[1]:
        if (rv==1).any():
            y = (rv==1).nonzero()[0][0]
            res[y, :] = 1
            return np.where(res==2, 0, res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 1 and cv[0, x] == 2:
                res[y, x] = 1
                y = (y+1) % N
        return np.where(res==2, 0, res)
    for y in range(N):
        for x in range(N):
            res[y, x] = (y^x)&1
    return res

def solve(N, row_prod_sum, col_prod_sum, row_val, col_val):
    res = np.zeros((N, N), dtype=np.uint64)
    for i in range(64):
        rps = row_prod_sum
        cps = col_prod_sum
        rv = row_val>>i&1
        cv = col_val>>i&1
        r = solve_1bit(N, rps, cps, rv, cv)
        res |= r<<i
    return 1, res
    #print(res)
    prod = res[0].copy()
    sum_ = res[0].copy()
    for y in range(1, N):
        prod &= res[y]
        sum_ |= res[y]
    #print(prod, sum_)
    if (prod[col_prod_sum==0] != col_val[col_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[col_prod_sum==1] != col_val[col_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    prod = res[:, 0].copy()
    sum_ = res[:, 0].copy()
    for x in range(1, N):
        prod &= res[:, x]
        sum_ |= res[:, x]
    #print(prod, sum_)
    if (prod[row_prod_sum==0] != row_val[row_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[row_prod_sum==1] != row_val[row_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    return 1, res



N = int(input())
row_prod_sum = np.array(input().split(), dtype=np.uint64)
col_prod_sum = np.array(input().split(), dtype=np.uint64)
row_val = np.array(input().split(), dtype=np.uint64)
col_val = np.array(input().split(), dtype=np.uint64)
t, ans = solve(N, row_prod_sum, col_prod_sum, row_val, col_val)
if t:
    for lst in ans.tolist():
        print(" ".join(map(str, lst)))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import numpy as np

def solve_1bit(N, rps, cps, rv, cv):
    res = np.full((N, N), 2, dtype=np.uint64)
    if N == 1:
        res[0, 0] = rv[0]
        return res
    r_all = [False, False]
    for i, (ps, v) in enumerate(zip(rps, rv)):
        if ps ^ v == 1:
            res[i, :] = v
            r_all[v] = True
    c_all = [False, False]
    for i, (ps, v) in enumerate(zip(cps, cv)):
        if ps ^ v == 1:
            res[:, i] = v
            c_all[v] = True
    if (r_all[0] and c_all[1]) or (r_all[1] and c_all[0]):
        return res
    if r_all[0] and c_all[0]:
        return np.where(res==2, 1, res)
    if r_all[1] and c_all[1]:
        return np.where(res==2, 0, res)
    if all(r_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = cv[x]
        return res
    if all(c_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = rv[y]
        return res
    return res
    if r_all[0]:
        if (cv==0).any():
            x = (cv==0).nonzero()[0][0]
            res[:, x] = 0
            return np.where(res==2, 1, res)
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 0 and rv[y, 0] == 2:
                res[y, x] = 0
                x = (x+1) % N
        return np.where(res==2, 1, res)
    if r_all[1]:
        if (cv==1).any():
            x = (cv==1).nonzero()[0][0]
            res[:, x] = 1
            return np.where(res==2, 0, res)
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 1 and rv[y, 0] == 2:
                res[y, x] = 1
                x = (x+1) % N
        return np.where(res==2, 0, res)
    if c_all[0]:
        if (rv==0).any():
            y = (rv==0).nonzero()[0][0]
            res[y, :] = 0
            return np.where(res==2, 1, res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 0 and cv[0, x] == 2:
                res[y, x] = 0
                y = (y+1) % N
        return np.where(res==2, 1, res)
    if c_all[1]:
        if (rv==1).any():
            y = (rv==1).nonzero()[0][0]
            res[y, :] = 1
            return np.where(res==2, 0, res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 1 and cv[0, x] == 2:
                res[y, x] = 1
                y = (y+1) % N
        return np.where(res==2, 0, res)
    for y in range(N):
        for x in range(N):
            res[y, x] = (y^x)&1
    return res

def solve(N, row_prod_sum, col_prod_sum, row_val, col_val):
    res = np.zeros((N, N), dtype=np.uint64)
    for i in range(64):
        rps = row_prod_sum
        cps = col_prod_sum
        rv = row_val>>i&1
        cv = col_val>>i&1
        r = solve_1bit(N, rps, cps, rv, cv)
        res |= r<<i
    return 1, res
    #print(res)
    prod = res[0].copy()
    sum_ = res[0].copy()
    for y in range(1, N):
        prod &= res[y]
        sum_ |= res[y]
    #print(prod, sum_)
    if (prod[col_prod_sum==0] != col_val[col_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[col_prod_sum==1] != col_val[col_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    prod = res[:, 0].copy()
    sum_ = res[:, 0].copy()
    for x in range(1, N):
        prod &= res[:, x]
        sum_ |= res[:, x]
    #print(prod, sum_)
    if (prod[row_prod_sum==0] != row_val[row_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[row_prod_sum==1] != row_val[row_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    return 1, res

Submission status: Wrong Answer

Solution code:
import numpy as np

def solve_1bit(N, rps, cps, rv, cv):
    res = np.full((N, N), 2, dtype=np.uint64)
    if N == 1:
        res[0, 0] = rv[0]
        return res
    r_all = [False, False]
    for i, (ps, v) in enumerate(zip(rps, rv)):
        if ps ^ v == 1:
            res[i, :] = v
            r_all[v] = True
    c_all = [False, False]
    for i, (ps, v) in enumerate(zip(cps, cv)):
        if ps ^ v == 1:
            res[:, i] = v
            c_all[v] = True
    return res
    if (r_all[0] and c_all[1]) or (r_all[1] and c_all[0]):
        return res
    if r_all[0] and c_all[0]:
        return np.where(res==2, 1, res)
    if r_all[1] and c_all[1]:
        return np.where(res==2, 0, res)
    if all(r_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = cv[x]
        return res
    if all(c_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = rv[y]
        return res
    if r_all[0]:
        if (cv==0).any():
            x = (cv==0).nonzero()[0][0]
            res[:, x] = 0
            return np.where(res==2, 1, res)
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 0 and rv[y, 0] == 2:
                res[y, x] = 0
                x = (x+1) % N
        return np.where(res==2, 1, res)
    if r_all[1]:
        if (cv==1).any():
            x = (cv==1).nonzero()[0][0]
            res[:, x] = 1
            return np.where(res==2, 0, res)
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 1 and rv[y, 0] == 2:
                res[y, x] = 1
                x = (x+1) % N
        return np.where(res==2, 0, res)
    if c_all[0]:
        if (rv==0).any():
            y = (rv==0).nonzero()[0][0]
            res[y, :] = 0
            return np.where(res==2, 1, res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 0 and cv[0, x] == 2:
                res[y, x] = 0
                y = (y+1) % N
        return np.where(res==2, 1, res)
    if c_all[1]:
        if (rv==1).any():
            y = (rv==1).nonzero()[0][0]
            res[y, :] = 1
            return np.where(res==2, 0, res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 1 and cv[0, x] == 2:
                res[y, x] = 1
                y = (y+1) % N
        return np.where(res==2, 0, res)
    for y in range(N):
        for x in range(N):
            res[y, x] = (y^x)&1
    return res

def solve(N, row_prod_sum, col_prod_sum, row_val, col_val):
    res = np.zeros((N, N), dtype=np.uint64)
    for i in range(64):
        rps = row_prod_sum
        cps = col_prod_sum
        rv = row_val>>i&1
        cv = col_val>>i&1
        r = solve_1bit(N, rps, cps, rv, cv)
        res |= r<<i
    return 1, res
    #print(res)
    prod = res[0].copy()
    sum_ = res[0].copy()
    for y in range(1, N):
        prod &= res[y]
        sum_ |= res[y]
    #print(prod, sum_)
    if (prod[col_prod_sum==0] != col_val[col_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[col_prod_sum==1] != col_val[col_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    prod = res[:, 0].copy()
    sum_ = res[:, 0].copy()
    for x in range(1, N):
        prod &= res[:, x]
        sum_ |= res[:, x]
    #print(prod, sum_)
    if (prod[row_prod_sum==0] != row_val[row_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[row_prod_sum==1] != row_val[row_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    return 1, res



N = int(input())
row_prod_sum = np.array(input().split(), dtype=np.uint64)
col_prod_sum = np.array(input().split(), dtype=np.uint64)
row_val = np.array(input().split(), dtype=np.uint64)
col_val = np.array(input().split(), dtype=np.uint64)
t, ans = solve(N, row_prod_sum, col_prod_sum, row_val, col_val)
if t:
    for lst in ans.tolist():
        print(" ".join(map(str, lst)))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:

"""

https://atcoder.jp/contests/abc164/tasks/abc164_f

各桁ごとに考えればよい
1.論理積が0 → 1つでも0があればおｋ → 0
2.論理積が1 → 全て1
2.論理和が0 → 全て0
1.論理和が1 → 1つでも1があればおk → 1

つまり、論路積が0,論理和が1を満たすような並べ方を見つければいい
そのような行・列が2つ以上ある場合は、01で市松模様で塗る
1つしかない場合、1でない方を見て行って、塗らなきゃいけない所だけ塗る
後は1つの方を見て行って、塗る

"""

from sys import stdin
import sys

N = int(stdin.readline())
S = list(map(int,stdin.readline().split()))
T = list(map(int,stdin.readline().split()))
U = list(map(int,stdin.readline().split()))
V = list(map(int,stdin.readline().split()))

ans = [[0] * N for i in range(N)]

for dig in range(64):
    nd = 2**dig

    a = [[None] * N for i in range(N)]

    #まず、論理積が0 / 論理和が1を処理する

    h = [] #横でグループ2に属する
    w = [] #縦でグル2

    for i in range(N):
        
        if S[i] == 0 and U[i]&nd > 0:
            for j in range(N):
                if a[i][j] == 0:
                    print (-1)
                    sys.exit()
                a[i][j] = 1
        
        elif S[i] == 1 and U[i]&nd == 0:
            for j in range(N):
                if a[i][j] == 1:
                    print (-1)
                    sys.exit()
                a[i][j] = 0
        elif S[i] == 0 and U[i]&nd == 0:
            h.append((i,0))
        else:
            h.append((i,1))

    for j in range(N):

        if T[j] == 0 and V[j]&nd > 0:
            for i in range(N):
                if a[i][j] == 0:
                    print (-1)
                    sys.exit()
                a[i][j] = 1
        elif T[j] == 1 and V[j]&nd == 0:
            for j in range(N):
                if a[i][j] == 1:
                    print (-1)
                    sys.exit()
                a[i][j] = 0
        elif T[j] == 0 and V[j]&nd == 0:
            w.append((j,0))
        else:
            w.append((j,1))

    #次に、h,wどちらも2以上の場合を処理する→交互に入れてけば終わり

    if len(h) > 1 and len(w) > 1:
        lside = 0
        now = 0
        for i,tmp1 in h:
            now = lside
            lside ^= 1
            for j,tmp2 in w:
                a[i][j] = now
                now ^= 1

    elif len(h) == 0 or len(w) == 0: #Noneがないので、ただしいかだけ確かめる

        for i,x in h:
            for j in range(N):
                if a[i][j] == x:
                    break
            else:
                print (-1)
                sys.exit()

        for j,x in w:
            for i in range(N):
                if a[i][j] == x:
                    break
            else:
                print (-1)
                sys.exit()

    elif len(w) == 1: #hの方を先に上手くやって、wの正当性を確かめる

        wj = w[0][0]
        
        for i,x in h:
            for j in range(N):
                if a[i][j] == x:
                    break
            else:
                a[i][wj] = x
                
        for j,x in w:
            flag = False
            for i in range(N):
                if a[i][j] == None:
                    a[i][j] = x
                if a[i][j] == x:
                    flag = True

            if not flag:
                print (-1)
                sys.exit()

    else: #wの方をうまくやってから、hの正当性を確かめる

        hi = h[0][0]

        for j,x in w:
            for i in range(N):
                if a[i][j] == x:
                    break
            else:
                a[hi][j] = x

        for i,x in h:
            flag = False
            for j in range(N):
                if a[i][j] == None:
                    a[i][j] = x
                if a[i][j] == x:
                    flag = True
            if not flag:
                print (-1)
                sys.exit()

    #print (a,h,w)
    for i in range(N):
        for j in range(N):
            if a[i][j] == None:
                a[i][j] = 0
            ans[i][j] += nd * a[i][j]

for i in ans:
    print (*i)