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:
# coding: utf-8
import sys
sysread = sys.stdin.readline
from heapq import heappop, heappush
from itertools import combinations
sys.setrecursionlimit(10**6)
import numpy as np
def run():
    # import sys
    # A = list(map(int, sys.stdin.readline().split()))
    # all = sys.stdin.readlines()
    #N, *A = map(int, open(0))
    #N = int(input())

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

    max_n = len(bin(max(U+V))[2:])
    def to_bin(x):
        x = bin(x)[2:]
        return "0" * (max_n - len(x)) + x

    U = list(map(to_bin, U))
    V = list(map(to_bin, V))


    Z = (np.zeros((N,N,max_n), dtype=np.int32) - 1)

    # fill absolutely determined columns
    row1 = np.array([[False] * max_n for _ in range(N)])
    col1 = np.array([[False] * max_n for _ in range(N)])
    row0 = np.array([[False] * max_n for _ in range(N)])
    col0 = np.array([[False] * max_n for _ in range(N)])
    row1_total = row1.sum(axis=0)
    row0_total = row0.sum(axis=0)
    col1_total = col1.sum(axis=0)
    col0_total = col0.sum(axis=0)


    for i, (s, val) in enumerate(zip(S,U)):
        if s == 0:# all 1
            for idx, k in enumerate(val):
                if k=="1":
                    Z[i, :, idx] = 1
                    row1[i, idx] = True
                    row1_total[idx] += True
        else:# all 0
            for idx, k in enumerate(val):
                if k=="0":
                    Z[i, :, idx] = 0
                    row0[i, idx] = True
                    row0_total[idx] += True

    for j, (t, val) in enumerate(zip(T,V)):
        if t == 0:# all 1
            for idx, k in enumerate(val):
                if k=="1":
                    Z[:, j, idx] = 1
                    col1[j, idx] = True
                    col1_total[idx] += True
        else:# all 0
            for idx, k in enumerate(val):
                if k=="0":
                    Z[:, j, idx] = 0
                    col0[j, idx] = True
                    col0_total[idx] += True

    for i in range(N):
        for idx in range(max_n):
            if col1[i,idx] and row0_total[idx]:
                print(-1)
                return None
            elif col0[i,idx] and row1_total[idx]:
                print(-1)
                return None
            elif row0[i,idx] and col1_total[idx]:
                print(-1)
                return None
            elif row1[i,idx] and col0_total[idx]:
                print(-1)
                return None


    # fill empty columns, according to sum-criteria
    for i, (s, val) in enumerate(zip(S,U)):
        if s == 1:# all 1
            for idx, v in enumerate(val):
                if v == '1':
                    if 1 in Z[i, :, idx]:continue
                    is_empty = [k for k,_v in enumerate(Z[i, :, idx]) if _v==-1]
                    if is_empty == []:
                        print(-1)
                        return None
                    for e in is_empty:
                        if T[e] == 0:
                            if 0 in Z[:, e, idx]:
                                Z[i, e, idx] = 1
                                break
                        else:
                            Z[i, e, idx] = 1
                            break

    for j, (t, val) in enumerate(zip(T, V)):
        if t == 1:  # all 1
            for idx, v in enumerate(val):
                if v == '1':
                    if 1 in Z[:, j, idx]: continue
                    is_empty = [k for k,_v in enumerate(Z[:, j, idx]) if _v == -1]
                    if is_empty == []:
                        print(-1)
                        return None
                    for e in is_empty:
                        if S[e] == 0:
                            if 0 in Z[e, :, idx]:
                                Z[e, j, idx] = 1
                                break
                        else:
                            Z[e, j, idx] = 1
                            break
    Z[Z==-1] = 0
    Z = Z.astype(str)

    A = [[0] * N for _ in range(N)]

    for i in range(N):
        for j in range(N):
            tmp = str(int(''.join(Z[i,j,:].tolist()), 2))
            A[i][j] = tmp


    for s in A:
        print(' '.join(s))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
sysread = sys.stdin.readline
from heapq import heappop, heappush
from itertools import combinations
sys.setrecursionlimit(10**6)
import numpy as np
def run():
    # import sys
    # A = list(map(int, sys.stdin.readline().split()))
    # all = sys.stdin.readlines()
    #N, *A = map(int, open(0))
    #N = int(input())


    process_length = lambda x:'0' * (max_n - len(str(x))) + str(x)
    N = int(sysread())
    S = list(map(int, sysread().split()))
    T = list(map(int, sysread().split()))
    U = list(map(int, sysread().split()))
    V = list(map(int, sysread().split()))

    max_n = len(bin(max(U+V))[2:])
    def to_bin(x):
        x = bin(x)[2:]
        return "0" * (max_n - len(x)) + x

    U = list(map(to_bin, U))
    V = list(map(to_bin, V))


    Z = (np.zeros((N,N,max_n), dtype=np.int32) - 1).astype(str)

    # fill absolutely determined columns
    row1 = np.array([[False] * max_n for _ in range(N)])
    col1 = np.array([[False] * max_n for _ in range(N)])
    row0 = np.array([[False] * max_n for _ in range(N)])
    col0 = np.array([[False] * max_n for _ in range(N)])

    for i, (s, val) in enumerate(zip(S,U)):
        if s == 0:# all 1
            for idx, k in enumerate(val):
                if k=="1":
                    Z[i, :, idx] = '1'
                    row1[i, idx] = True
        else:# all 0
            for idx, k in enumerate(val):
                if k=="0":
                    Z[i, :, idx] = '0'
                    row0[i, idx] = True

    for j, (t, val) in enumerate(zip(T,V)):
        if t == 0:# all 1
            for idx, k in enumerate(val):
                if k=="1":
                    Z[:, j, idx] = '1'
                    col1[j, idx] = True
        else:# all 0
            for idx, k in enumerate(val):
                if k=="0":
                    Z[:, j, idx] = '0'
                    col0[j, idx] = True

    for i in range(N):
        for idx in range(max_n):
            if col1[i,idx] and row0[:,idx].sum():
                print(-1)
                return None
            elif col0[i,idx] and row1[:,idx].sum():
                print(-1)
                return None
            elif row0[i,idx] and col1[:,idx].sum():
                print(-1)
                return None
            elif row1[i,idx] and col0[:,idx].sum():
                print(-1)
                return None


    # fill empty columns, according to sum-criteria
    for i, (s, val) in enumerate(zip(S,U)):
        if s == 1:# all 1
            for idx, v in enumerate(val):
                if v == '1':
                    if '1' in Z[i, :, idx]:continue
                    is_empty = [k for k,_v in enumerate(Z[i, :, idx]) if _v=='-1']
                    if is_empty == []:
                        print(-1)
                        return None
                    for e in is_empty:
                        if T[e] == 0:
                            if (Z[:, e, idx] == '0').sum() > 0:
                                Z[i, e, idx] = '1'
                                break
                        else:
                            Z[i, e, idx] = '1'
                            break

    for j, (t, val) in enumerate(zip(T, V)):
        if t == 1:  # all 1
            for idx, v in enumerate(val):
                if v == '1':
                    if '1' in Z[:, j, idx]: continue
                    is_empty = [k for k,_v in enumerate(Z[:, j, idx]) if _v == '-1']
                    if is_empty == []:
                        print(-1)
                        return None
                    for e in is_empty:
                        if S[e] == 0:
                            if (Z[e, :, idx] == '0').sum() > 0:
                                Z[e, j, idx] = '1'
                                break
                        else:
                            Z[e, j, idx] = '1'
                            break
    Z[Z=='-1'] = 0

    A = [[0] * N for _ in range(N)]

    for i in range(N):
        for j in range(N):
            tmp = str(int(''.join(Z[i,j,:].tolist()), 2))
            A[i][j] = tmp


    for s in A:
        print(' '.join(s))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
sysread = sys.stdin.readline
from heapq import heappop, heappush
from itertools import combinations
sys.setrecursionlimit(10**6)
import numpy as np
def run():
    # import sys
    # A = list(map(int, sys.stdin.readline().split()))
    # all = sys.stdin.readlines()
    #N, *A = map(int, open(0))
    #N = int(input())


    process_length = lambda x:'0' * (max_n - len(str(x))) + str(x)
    N = int(sysread())
    S = list(map(int, sysread().split()))
    T = list(map(int, sysread().split()))
    U = list(map(int, sysread().split()))
    V = list(map(int, sysread().split()))

    max_n = len(bin(max(U+V))[2:])
    def to_bin(x):
        x = bin(x)[2:]
        return "0" * (max_n - len(x)) + x

    U = list(map(to_bin, U))
    V = list(map(to_bin, V))


    Z = (np.zeros((N,N,max_n), dtype=np.int32) - 1).astype(str)

    # fill absolutely determined columns
    for i, (s, val) in enumerate(zip(S,U)):
        if s == 0:# all 1
            for idx, k in enumerate(val):
                if k=="1":
                    for j in range(N):
                        Z[i, j, idx] = '1'
        else:# all 0
            for idx, k in enumerate(val):
                if k=="0":
                    for j in range(N):
                        if Z[i, j, idx] == '1':
                            print(-1)
                            return
                        Z[i, j, idx] = '0'

    for j, (t, val) in enumerate(zip(T,V)):
        if t == 0:# all 1
            for idx, k in enumerate(val):
                if k=="1":
                    for i in range(N):
                        if Z[i, j, idx] == '0':
                            print(-1)
                            return
                        Z[i, j, idx] = '1'
        else:# all 0
            for idx, k in enumerate(val):
                if k=="0":
                    print(idx, k)
                    for i in range(N):
                        if Z[i, j, idx] == '1':
                            print(-1)
                            return
                        Z[i, j, idx] = '0'

    # fill empty columns, according to sum-criteria
    for i, (s, val) in enumerate(zip(S,U)):
        if s == 1:# all 1
            for idx, v in enumerate(val):
                if v == '1':
                    if '1' in Z[i, :, idx]:continue
                    is_empty = [k for k,_v in enumerate(Z[i, :, idx]) if _v=='-1']
                    if is_empty == []:
                        return print(-1)
                    for e in is_empty:
                        if T[e] == 0:
                            if (Z[:, e, idx] == '0').sum() > 0:
                                Z[i, e, idx] = '1'
                                break
                        else:
                            Z[i, e, idx] = '1'
                            break

    for j, (t, val) in enumerate(zip(T, V)):
        if t == 1:  # all 1
            for idx, v in enumerate(val):
                if v == '1':
                    if '1' in Z[:, j, idx]: continue
                    is_empty = [k for k,_v in enumerate(Z[:, j, idx]) if _v == '-1']
                    if is_empty == []:
                        return print(-1)
                    for e in is_empty:
                        if S[e] == 0:
                            if (Z[e, :, idx] == '0').sum() > 0:
                                Z[e, j, idx] = '1'
                                break
                        else:
                            Z[e, j, idx] = '1'
                            break
    Z[Z=='-1'] = 0

    A = [[0] * N for _ in range(N)]

    for i in range(N):
        for j in range(N):
            tmp = str(int(''.join(Z[i,j,:].tolist()), 2))
            A[i][j] = tmp


    for s in A:
        print(' '.join(s))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def main():
    n = int(input())
    ans = [[0 for _ in range(n)] for _ in range(n)]
    s = list(map(int, input().split()))
    t = list(map(int, input().split()))
    u = list(map(int, input().split()))
    v = list(map(int, input().split()))
    for i in range(n):
        if not s[i]:
            for j in range(n):
                ans[i][j] = ans[i][j]|u[i]
        if not t[i]:   
            for j in range(n):
                ans[j][i] = ans[j][i]|v[i]
    for i in range(n):
        for j in range(n):
            if (u[i] & v[j]):
                ans[i][j] = ans[i][j]|(u[i]&v[j])
    for i in range(n):
        check_xs = 0
        check_ys = 0
        check_xm = ans[i][0]
        check_ym = ans[0][i]
        for j in range(n):
            check_xs = check_xs|ans[i][j]
            check_ys = check_ys|ans[j][i]
            check_xm = check_xm&ans[i][j]
            check_ym = check_ym&ans[j][i]
    for x in range(n):
        if not s[x]:
            continue
        x_sum = ans[x][0]
        for y in range(n):
            x_sum = x_sum|ans[x][y]
        if x_sum == u[x]: 
            continue
        up = u[x] - x_sum
        for y in range(n):
            if t[y]:
                continue
            y_mul = ans[0][y]
            for i in range(n):
                if i == x:
                    continue
                y_mul = y_mul&ans[i][y]
            up_y = (~y_mul)&up
            ans[x][y] += up_y
            up -= up_y
            if not up_y:
                break
    for i in range(n):
        check_xs = 0
        check_ys = 0
        check_xm = ans[i][0]
        check_ym = ans[0][i]
        for j in range(n):
            check_xs = check_xs|ans[i][j]
            check_ys = check_ys|ans[j][i]
            check_xm = check_xm&ans[i][j]
            check_ym = check_ym&ans[j][i]
    for y in range(n):
        if not t[y]:
            continue
        y_sum = ans[0][y]
        for x in range(n):
            y_sum = y_sum|ans[x][y]
        if y_sum == v[y]: 
            continue
        up = v[y] - y_sum
        for x in range(n):
            if s[x]:
                continue
            x_mul = ans[x][0]
            for j in range(n):
                if y == j:
                    continue
                x_mul = x_mul&ans[x][j]
            up_x = (~x_mul)&up
            ans[x][y] += up_x
            up -= up_x
            if not up_x:
                break
    for i in range(n):
        check_xs = 0
        check_ys = 0
        check_xm = ans[i][0]
        check_ym = ans[0][i]
        for j in range(n):
            check_xs = check_xs|ans[i][j]
            check_ys = check_ys|ans[j][i]
            check_xm = check_xm&ans[i][j]
            check_ym = check_ym&ans[j][i]
        if s[i] and u[i] != check_xs:
            print(-1)
            return       
        if t[i] and v[i] != check_ys:
            print(-1)
            return       
        if not s[i] and u[i] != check_xm:
            print(-1)
            return       
        if not t[i] and v[i] != check_ym:
            print(-1)
            return
    for i in range(n):
        print(*ans[i])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def main():
    n = int(input())
    ans = [[0 for _ in range(n)] for _ in range(n)]
    s = list(map(int, input().split()))
    t = list(map(int, input().split()))
    u = list(map(int, input().split()))
    v = list(map(int, input().split()))
    for i in range(n):
        if not s[i]:
            for j in range(n):
                ans[i][j] = ans[i][j]|u[i]
        if not t[i]:   
            for j in range(n):
                ans[j][i] = ans[j][i]|v[i]
    for i in range(n):
        for j in range(n):
            if (u[i] & v[j]):
                ans[i][j] = ans[i][j]|(u[i]&v[j])
    for x in range(n):
        if not s[x]:
            continue
        x_sum = ans[x][0]
        for y in range(n):
            x_sum = x_sum|ans[x][y]
        if x_sum == u[x]: 
            continue
        up = u[x] - x_sum
        for y in range(n):
            if t[y]:
                continue
            y_mul = ans[0][y]
            for i in range(n):
                y_mul = y_mul&ans[i][y]
            up_y = (~y_mul)&up
            ans[x][y] += up_y
            up -= up_y
            if not up_y:
                break
    for y in range(n):
        if not t[y]:
            continue
        y_sum = ans[0][y]
        for x in range(n):
            y_sum = y_sum|ans[x][y]
        if y_sum == v[y]: 
            continue
        up = v[y] - y_sum
        for x in range(n):
            if s[x]:
                continue
            x_mul = ans[x][0]
            for j in range(n):
                x_mul = x_mul&ans[x][j]
            up_x = (~x_mul)&up
            ans[x][y] += up_x
            up -= up_x
            if not up_x:
                break
    for i in range(n):
        check_xs = 0
        check_ys = 0
        check_xm = ans[i][0]
        check_ym = ans[0][i]
        for j in range(n):
            check_xs = check_xs|ans[i][j]
            check_ys = check_ys|ans[j][i]
            check_xm = check_xm&ans[i][j]
            check_ym = check_ym&ans[j][i]
        if s[i] and u[i] != check_xs:
            print(-1)
            return       
        if t[i] and v[i] != check_ys:
            print(-1)
            return       
        if not s[i] and u[i] != check_xm:
            print(-1)
            return       
        if not t[i] and v[i] != check_ym:
            print(-1)
            return
    for i in range(n):
        print(*ans[i])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env pypy3
import sys


def solve(N: int, S: "List[int]", T: "List[int]", U: "List[int]", V: "List[int]"):
    AND, ORE = 0, 1
    ans = [[0 for j in range(N)] for i in range(N)]
    can = True
    for b in range(64):
        parityU = [(x >> b) & 1 for x in U]
        parityV = [(x >> b) & 1 for x in V]
        can &= all(not (S[i] == ORE and parityU[i] == 0 and T[j] == AND and parityV[j] == 1) for i in range(N) for j in range(N))
        can &= all(not (S[i] == AND and parityU[i] == 1 and T[j] == ORE and parityV[j] == 0) for i in range(N) for j in range(N))
        for i in range(N):
            if S[i] == AND and parityU[i] == 1:
                for j in range(N):
                    ans[i][j] |= 1 << b
        for j in range(N):
            if T[j] == AND and parityV[j] == 1:
                for i in range(N):
                    ans[i][j] |= 1 << b
        rows_need0 = [i for i in range(N) if S[i] == AND and parityU[i] == 0] if all(parityV[j] == 1 for j in range(N)) else []
        rows_need1 = [i for i in range(N) if S[i] == ORE and parityU[i] == 1] if all(parityV[j] == 0 for j in range(N)) else []
        cols_need0 = [j for j in range(N) if T[j] == AND and parityV[j] == 0] if all(parityU[i] == 1 for i in range(N)) else []
        cols_need1 = [j for j in range(N) if T[j] == ORE and parityV[j] == 1] if all(parityU[i] == 0 for i in range(N)) else []
        can &= not rows_need0 or any(T[j] == ORE for j in range(N))
        can &= not rows_need1 or any(T[j] == AND for j in range(N))
        can &= not cols_need0 or any(S[i] == ORE for i in range(N))
        can &= not cols_need1 or any(S[i] == AND for i in range(N))
        can &= not any(len(a) == 1 and len(b) > 0 or len(b) == 1 and len(a) > 0 for a, b in ((rows_need0, cols_need1), (rows_need1, cols_need0)))
        for rows, cols in ((rows_need0, cols_need1), (rows_need1, cols_need0)):
            for ii, i in enumerate(rows):
                for jj, j in enumerate(cols):
                    ans[i][j] |= ((ii & 1) ^ (jj & 1)) << b
    if can:
        for row in ans:
            print(*row)
    else:
        print(-1)


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    N = int(sys.stdin.readline())  # type: int
    S = [int(x) for x in sys.stdin.readline().split()]  # type: "List[int]"
    T = [int(x) for x in sys.stdin.readline().split()]  # type: "List[int]"
    U = [int(x) for x in sys.stdin.readline().split()]  # type: "List[int]"
    V = [int(x) for x in sys.stdin.readline().split()]  # type: "List[int]"
    solve(N, S, T, U, V)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env pypy3
import sys
from functools import reduce
from operator import or_, and_


def solve(N: int, S: "List[int]", T: "List[int]", U: "List[int]", V: "List[int]"):
    AND, ORE = 0, 1
    ans = [[0 for j in range(N)] for i in range(N)]
    can = True
    for b in range(64):
        parityU = [(x >> b) & 1 for x in U]
        parityV = [(x >> b) & 1 for x in V]
        can &= all(not (S[i] == ORE and parityU[i] == 0 and T[j] == AND and parityV[j] == 1) for i in range(N) for j in range(N))
        can &= all(not (S[i] == AND and parityU[i] == 1 and T[j] == ORE and parityV[j] == 0) for i in range(N) for j in range(N))
        for i in range(N):
            if S[i] == AND and parityU[i] == 1:
                for j in range(N):
                    ans[i][j] |= 1 << b
        for j in range(N):
            if T[j] == AND and parityV[j] == 1:
                for i in range(N):
                    ans[i][j] |= 1 << b
        rows_free0 = [i for i in range(N) if parityU[i] == 0 or S[i] == ORE and any(x == 1 for x in parityV)]
        rows_free1 = [i for i in range(N) if parityU[i] == 1 or S[i] == AND and any(x == 0 for x in parityV)]
        cols_free0 = [j for j in range(N) if parityV[j] == 0 or T[j] == ORE and any(x == 1 for x in parityU)]
        cols_free1 = [j for j in range(N) if parityV[j] == 1 or T[j] == AND and any(x == 0 for x in parityU)]
        rows_need0 = [i for i in range(N) if S[i] == AND and parityU[i] == 0] if not cols_free0 else []
        rows_need1 = [i for i in range(N) if S[i] == ORE and parityU[i] == 1]
        cols_need0 = [j for j in range(N) if T[j] == AND and parityV[j] == 0] if not rows_free0 else []
        cols_need1 = [j for j in range(N) if T[j] == ORE and parityV[j] == 1]
        if rows_free1:
            for col in cols_need1:
                ans[rows_free1[0]][col] |= 1 << b
            cols_need1 = []
        if cols_free1:
            for row in rows_need1:
                ans[row][cols_free1[0]] |= 1 << b
            rows_need1 = []
        can &= not rows_need0 or any(T[j] == ORE for j in range(N))
        can &= not rows_need1 or any(T[j] == AND for j in range(N))
        can &= not cols_need0 or any(S[i] == ORE for i in range(N))
        can &= not cols_need1 or any(S[i] == AND for i in range(N))
        can &= not any(len(a) == 1 and len(b) > 0 or len(b) == 1 and len(a) > 0 for a, b in ((rows_need0, cols_need1), (rows_need1, cols_need0)))
        for rows, cols in ((rows_need0, cols_need1), (cols_need0, rows_need1)):
            assert not can or len(rows) == 0 and len(cols) == 0 or len(rows) > 1 and len(cols) > 1
            for ii, i in enumerate(rows):
                for jj, j in enumerate(cols):
                    ans[i][j] |= ((ii & 1) ^ (jj & 1)) << b
    if can:
        for row in ans:
            print(*row)
        # assert all(reduce(or_ if S[i] else and_, (ans[i][j] for j in range(N))) == U[i] for i in range(N))
        # assert all(reduce(or_ if T[j] else and_, (ans[i][j] for i in range(N))) == V[j] for j in range(N))
    else:
        print(-1)


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    N = int(sys.stdin.readline())  # type: int
    S = [int(x) for x in sys.stdin.readline().split()]  # type: "List[int]"
    T = [int(x) for x in sys.stdin.readline().split()]  # type: "List[int]"
    U = [int(x) for x in sys.stdin.readline().split()]  # type: "List[int]"
    V = [int(x) for x in sys.stdin.readline().split()]  # type: "List[int]"
    solve(N, S, T, U, V)

if __name__ == '__main__':
    from time import time
    start = time()
    main()
    end = time()
    print(end - start, file=sys.stderr)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env pypy3
import sys
from functools import reduce
from operator import or_, and_


def solve(N: int, S: "List[int]", T: "List[int]", U: "List[int]", V: "List[int]"):
    AND, ORE = 0, 1
    ans = [[0 for j in range(N)] for i in range(N)]
    can = True
    for b in range(64):
        parityU = [(x >> b) & 1 for x in U]
        parityV = [(x >> b) & 1 for x in V]
        can &= all(not (S[i] == ORE and parityU[i] == 0 and T[j] == AND and parityV[j] == 1) for i in range(N) for j in range(N))
        can &= all(not (S[i] == AND and parityU[i] == 1 and T[j] == ORE and parityV[j] == 0) for i in range(N) for j in range(N))
        for i in range(N):
            if S[i] == AND and parityU[i] == 1:
                for j in range(N):
                    ans[i][j] |= 1 << b
        for j in range(N):
            if T[j] == AND and parityV[j] == 1:
                for i in range(N):
                    ans[i][j] |= 1 << b
        rows_free0 = [i for i in range(N) if parityU[i] == 0 or S[i] == ORE and any(x == 1 for x in parityV)]
        rows_free1 = [i for i in range(N) if parityU[i] == 1 or S[i] == AND and any(x == 0 for x in parityV)]
        cols_free0 = [j for j in range(N) if parityV[j] == 0 or T[j] == ORE and any(x == 1 for x in parityU)]
        cols_free1 = [j for j in range(N) if parityV[j] == 1 or T[j] == AND and any(x == 0 for x in parityU)]
        rows_need0 = [i for i in range(N) if S[i] == AND and parityU[i] == 0] if not cols_free0 else []
        rows_need1 = [i for i in range(N) if S[i] == ORE and parityU[i] == 1]
        cols_need0 = [j for j in range(N) if T[j] == AND and parityV[j] == 0] if not rows_free0 else []
        cols_need1 = [j for j in range(N) if T[j] == ORE and parityV[j] == 1]
        if rows_free1:
            for col in cols_need1:
                ans[rows_free1[0]][col] |= 1 << b
            cols_need1 = []
        if cols_free1:
            for row in rows_need1:
                ans[row][cols_free1[0]] |= 1 << b
            rows_need1 = []
        can &= not rows_need0 or any(T[j] == ORE for j in range(N))
        can &= not rows_need1 or any(T[j] == AND for j in range(N))
        can &= not cols_need0 or any(S[i] == ORE for i in range(N))
        can &= not cols_need1 or any(S[i] == AND for i in range(N))
        can &= not any(len(a) == 1 and len(b) > 0 or len(b) == 1 and len(a) > 0 for a, b in ((rows_need0, cols_need1), (rows_need1, cols_need0)))
        for rows, cols in ((rows_need0, cols_need1), (cols_need0, rows_need1)):
            # assert len(rows) == 0 and len(cols) == 0 or len(rows) > 1 and len(cols) > 1
            for ii, i in enumerate(rows):
                for jj, j in enumerate(cols):
                    ans[i][j] |= ((ii & 1) ^ (jj & 1)) << b
    if can:
        for row in ans:
            print(*row)
        # assert all(reduce(or_ if S[i] else and_, (ans[i][j] for j in range(N))) == U[i] for i in range(N))
        # assert all(reduce(or_ if T[j] else and_, (ans[i][j] for i in range(N))) == V[j] for j in range(N))
    else:
        print(-1)


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    N = int(sys.stdin.readline())  # type: int
    S = [int(x) for x in sys.stdin.readline().split()]  # type: "List[int]"
    T = [int(x) for x in sys.stdin.readline().split()]  # type: "List[int]"
    U = [int(x) for x in sys.stdin.readline().split()]  # type: "List[int]"
    V = [int(x) for x in sys.stdin.readline().split()]  # type: "List[int]"
    solve(N, S, T, U, V)

if __name__ == '__main__':
    from time import time
    start = time()
    main()
    end = time()
    print(end - start, file=sys.stderr)