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:
n = int(input())
s = [int(x) for x in input().split()]
t = [int(x) for x in input().split()]
u = [int(x) for x in input().split()]
v = [int(x) for x in input().split()]


def bit(n, k): return (n >> k) & 1


a = [[0]*n for _ in range(n)]

for k in range(64):
    fixed_row = [False]*n
    fixed_column = [False]*n

    # processing for rows
    for i in range(n):
        if s[i] == 0 and bit(u[i], k) == 1:
            fixed_row[i] = True
            for j in range(n):
                a[i][j] += 1 << k
        elif s[i] == 1 and bit(u[i], k) == 0:
            fixed_row[i] = True
            for j in range(n):
                a[i][j] += 0 << k
        else:  # s[i] == bit(u[i], k)
            for j in range(n):
                a[i][j] += s[i] << k

    # processing for columns
    for j in range(n):
        if t[j] == 0 and bit(v[j], k) == 1:
            fixed_column[j] = True
            for i in range(n):
                if fixed_row[i] and bit(a[i][j], k) == 0:
                    print(-1)
                    exit()
                else:
                    a[i][j] |= 1 << k
        elif t[j] == 1 and bit(v[j], k) == 0:
            fixed_column[j] = True
            for i in range(n):
                if fixed_row[i] and bit(a[i][j], k) == 1:
                    print(-1)
                    exit()
        else:  # t[j] == bit(v[j], k)
            if t[j] == 0 and all(bit(a[i][j], k) == 1 for i in range(n)):
                err = True
                for i in range(n):
                    if not fixed_row[i] and [bit(a[i][j], k) for j in range(n)].count(1) >= 2:
                        a[i][j] ^= (1 << k)
                        err = False
                if err:
                    print(-1)
                    exit()
            elif t[j] == 1 and all(bit(a[i][j], k) == 0 for i in range(n)):
                err = True
                for i in range(n):
                    if not fixed_row[i] and [bit(a[i][j], k) for j in range(n)].count(0) >= 2:
                        a[i][j] ^= (1 << k)
                        err = False
                if err:
                    print(-1)
                    exit()

for i in range(len(a)):
    print(*a[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [int(x) for x in input().split()]
t = [int(x) for x in input().split()]
u = [int(x) for x in input().split()]
v = [int(x) for x in input().split()]


def bit(n, k): return (n >> k) & 1


a = [[0]*n for _ in range(n)]

for k in range(64):
    fixed_row = [False]*n
    fixed_column = [False]*n

    # processing for rows
    for i in range(n):
        if s[i] == 0 and bit(u[i], k) == 1:
            fixed_row[i] = True
            for j in range(n):
                a[i][j] += 1 << k
        elif s[i] == 1 and bit(u[i], k) == 0:
            fixed_row[i] = True
            for j in range(n):
                a[i][j] += 0 << k

    # processing for columns
    for j in range(n):
        if t[j] == 0 and bit(v[j], k) == 1:
            fixed_column[j] = True
            for i in range(n):
                if fixed_row[i] and bit(a[i][j], k) == 0:
                    print(-1)
                    exit()
        elif t[j] == 1 and bit(v[j], k) == 0:
            fixed_column[j] = True
            for i in range(n):
                if fixed_row[i] and bit(a[i][j], k) == 1:
                    print(-1)
                    exit()
        else:  # t[j] == bit(v[j], k)
            if t[j] == 0 and all(bit(a[i][j], k) == 1 for i in range(n)):
                print(-1)
                exit()
            elif t[j] == 1 and all(bit(a[i][j], k) == 0 for i in range(n)):
                err = True
                for i in range(n):
                    if not fixed_row[i] and [bit(a[i][j], k) for j in range(n)].count(0) >= 2:
                        a[i][j] ^= (1 << k)
                        err = False
                if err:
                    print(-1)
                    exit()

for i in range(len(a)):
    print(*a[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [int(x) for x in input().split()]
t = [int(x) for x in input().split()]
u = [int(x) for x in input().split()]
v = [int(x) for x in input().split()]


def bit(n, k): return (n >> k) & 1


a = [[0]*n for _ in range(n)]

for k in range(64):
    fixed_row = [False]*n
    fixed_column = [False]*n

    # processing for rows
    for i in range(n):
        if s[i] == 0 and bit(u[i], k) == 1:
            fixed_row[i] = True
            for j in range(n):
                a[i][j] += 1 << k
        elif s[i] == 1 and bit(u[i], k) == 0:
            fixed_row[i] = True
            for j in range(n):
                a[i][j] += 0 << k
        else:  # s[i] == bit(u[i], k)
            for j in range(n):
                a[i][j] += s[i] << k

    # processing for columns
    for j in range(n):
        if t[j] == 0 and bit(v[j], k) == 1:
            fixed_column[j] = True
            for i in range(n):
                if fixed_row[i] and bit(a[i][j], k) == 0:
                    print(-1)
                    exit()
        elif t[j] == 1 and bit(v[j], k) == 0:
            fixed_column[j] = True
            for i in range(n):
                if fixed_row[i] and bit(a[i][j], k) == 1:
                    print(-1)
                    exit()
        else:  # t[j] == bit(v[j], k)
            if t[j] == 0 and all(bit(a[i][j], k) == 1 for i in range(n)):
                err = True
                for i in range(n):
                    if not fixed_row[i] and [bit(a[i][j], k) for j in range(n)].count(1) >= 2:
                        a[i][j] ^= (1 << k)
                        fixed_row[i] = True
                        err = False
                        break
                if err:
                    print(-1)
                    exit()
            elif t[j] == 1 and all(bit(a[i][j], k) == 0 for i in range(n)):
                err = True
                for i in range(n):
                    if not fixed_row[i] and [bit(a[i][j], k) for j in range(n)].count(0) >= 2:
                        a[i][j] ^= (1 << k)
                        fixed_row[i] = True
                        err = False
                        break
                if err:
                    print(-1)
                    exit()

for i in range(len(a)):
    print(*a[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [int(x) for x in input().split()]
t = [int(x) for x in input().split()]
u = [int(x) for x in input().split()]
v = [int(x) for x in input().split()]


def bit(n, k): return (n >> k) & 1


a = [[0]*n for _ in range(n)]

for k in range(64):
    fixed_row = [False]*n
    fixed_column = [False]*n

    # processing for rows
    for i in range(n):
        if s[i] == 0 and bit(u[i], k) == 1:
            fixed_row[i] = True
            for j in range(n):
                a[i][j] += 1 << k
        elif s[i] == 1 and bit(u[i], k) == 0:
            fixed_row[i] = True
            for j in range(n):
                a[i][j] += 0 << k
        else:  # s[i] == bit(u[i], k)
            for j in range(n):
                a[i][j] += s[i] << k

    # processing for columns
    for j in range(n):
        if t[j] == 0 and bit(v[j], k) == 1:
            fixed_column[j] = True
            for i in range(n):
                if fixed_row[i] and bit(a[i][j], k) == 0:
                    print(-1)
                    exit()
        elif t[j] == 1 and bit(v[j], k) == 0:
            fixed_column[j] = True
            for i in range(n):
                if fixed_row[i] and bit(a[i][j], k) == 1:
                    print(-1)
                    exit()
        else:  # t[j] == bit(v[j], k)
            if t[j] == 0 and all(bit(a[i][j], k) == 1 for i in range(n)):
                err = True
                for i in range(n):
                    if not fixed_row[i]:
                        a[i][j] ^= (1 << k)
                        fixed_row[i] = True
                        err = False
                        break
                if err:
                    print(-1)
                    exit()
            elif t[j] == 1 and all(bit(a[i][j], k) == 0 for i in range(n)):
                err = True
                for i in range(n):
                    if not fixed_row[i]:
                        a[i][j] ^= (1 << k)
                        fixed_row[i] = True
                        err = False
                        break
                if err:
                    print(-1)
                    exit()

for i in range(len(a)):
    print(*a[i])

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 list("0" * (max_n - len(x)) + x)

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


    Z = np.zeros((N,N), dtype=np.uint64)
    Z_bool = np.zeros((N,N,max_n), dtype='bool')

    # 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,:] += 1 << (max_n-idx-1)
                    Z_bool[i,:,idx] = True
                    row1[i, idx] = True
                    row1_total[idx] += True
        else:# all 0
            for idx, k in enumerate(val):
                if k=="0":
                    Z_bool[i, :, idx] = True
                    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] += 1 << (max_n-idx-1)
                    Z_bool[:,j,idx] = True
                    col1[j, idx] = True
                    col1_total[idx] += True
        else:# all 0
            for idx, k in enumerate(val):
                if k=="0":
                    Z_bool[:,j,idx] = True
                    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]) or (col0[i,idx] and row1_total[idx]) or \
                    (row0[i,idx] and col1_total[idx]) or (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 row1[i,idx]:continue
                    is_empty = [k for k,_v in enumerate(Z_bool[i, :, idx]) if not _v]
                    if is_empty == []:
                        print(-1)
                        return None
                    for e in is_empty:
                        if T[e] == 0:
                            if col0[e, idx]:
                                Z[i, e] += 1 << (max_n-idx-1)
                                Z_bool[i, e, idx] = True
                                col1[e,idx], row1[i,idx] = True, True
                                break
                        else:
                            Z[i, e] += 1 << (max_n - idx-1)
                            col1[e, idx], row1[i, idx] = True, True
                            Z_bool[i,e,idx] = True
                            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 col1[j,idx]: continue
                    is_empty = [k for k,_v in enumerate(Z_bool[:, j, idx]) if not _v]
                    if is_empty == []:
                        print(-1)
                        return None
                    for e in is_empty:
                        if S[e] == 0:
                            if row0[e,idx]:
                                Z[e, j] += 1 << (max_n-idx-1)
                                row1[e, idx], col1[i, idx] = True, True
                                Z_bool[e,j,idx] = True
                                break
                        else:
                            Z[e, j] += 1 << (max_n - idx -1)
                            Z_bool[e,j,idx] = True
                            row1[e, idx], col1[i, idx] = True, True
                            break

    Z = Z.tolist()

    for z in Z:
        for s in z:
            print("{} ".format(int(s)), end="")
        print("")

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(map(int,input().split()))
T = list(map(int,input().split()))
U = list(map(int,input().split()))
V = list(map(int,input().split()))

ans = [[0]*N for _ in range(N)]
possible = True
for n in range(64):
    n = 2**n
    ud = {(0,0):0,(0,1):0,(1,0):0,(1,1):0}
    vd = {(0,0):0,(0,1):0,(1,0):0,(1,1):0}
    for i in range(N):
        s = S[i]
        u = U[i]&n
        if u:
            u = 1
        ud[(s,u)] += 1
    for j in range(N):
        t = T[j]
        v = V[j]&n
        if v:
            v = 1
        vd[(t,v)] += 1
    for i in range(N):
        s = S[i]
        u = U[i]&n
        if u>0:
            u = 1
        for j in range(N):
            t = T[j]
            v = V[j]&n
            if v>0:
                v = 1
            if u==v:
                ans[i][j] += u*n
            else:
                if (s,u)==(0,1) and (t,v)==(1,0):
                    possible = False
                    break
                if (s,u)==(1,0) and (t,v)==(0,1):
                    possible = False
                    break
                if (s,u)==(0,1) or (t,v)==(0,1):
                    ans[i][j] += n
    if ud[(0,0)]+ud[(1,0)]==N:
        if ud[(0,0)]<=1 and vd[(1,1)]==N:
            possible = False
        elif ud[(0,0)]==0 and vd[(1,1)]>0:
            possible = False
        elif vd[(1,1)]<N:
            for i in range(N):
                s = S[i]
                u = U[i]&n
                if u>0:
                    u = 1
                if (s,u)==(0,0):
                    for j in range(N):
                        t = T[j]
                        v = V[j]&n
                        if v:
                            v = 1
                        if (t,v)==(1,1):
                            ans[i][j] += n
        else:
            c = 0
            for i in range(N):
                s = S[i]
                u = U[i]&n
                if u>0:
                    u = 1
                if (s,u)==(0,0) and c==0:
                    for j in range(N):
                        t = T[j]
                        v = V[j]&n
                        if v:
                            v = 1
                        if (t,v)==(1,1):
                            if c==0:
                                c += 1
                            else:
                                ans[i][j] += n
                elif (s,u)==(0,0) and c==1:
                    for j in range(N):
                        t = T[j]
                        v = V[j]&n
                        if v:
                            v = 1
                        if (t,v)==(1,1):
                            ans[i][j] += n
                            c += 1
                            break
                if c==2:
                    break
    if vd[(0,0)]+vd[(1,0)]==N:
        if vd[(0,0)]<=1 and ud[(1,1)]==N:
            possible = False
        elif vd[(0,0)]==0 and ud[(1,1)]>0:
            possible = False
        elif ud[(1,1)]<N:
            for j in range(N):
                t = T[j]
                v = V[j]&n
                if v>0:
                    v = 1
                if (t,v)==(0,0):
                    for i in range(N):
                        s = S[i]
                        u = U[i]&n
                        if u:
                            u = 1
                        if (s,u)==(1,1):
                            ans[i][j] += n
        else:
            c = 0
            for j in range(N):
                t = T[j]
                v = V[j]&n
                if v>0:
                    v = 1
                if (s,v)==(0,0) and c==0:
                    for i in range(N):
                        s = S[i]
                        u = U[i]&n
                        if u:
                            u = 1
                        if (s,u)==(1,1):
                            if c==0:
                                c += 1
                            else:
                                ans[i][j] += n
                elif (t,v)==(0,0) and c==1:
                    for i in range(N):
                        s = S[i]
                        u = U[i]&n
                        if u:
                            u = 1
                        if (s,u)==(1,1):
                            ans[i][j] += n
                            c += 1
                            break
                if c==2:
                    break
            
                    

            
    if not possible:
        break

if possible:
    for i in range(N):
        print(*ans[i])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(map(int,input().split()))
T = list(map(int,input().split()))
U = list(map(int,input().split()))
V = list(map(int,input().split()))

ans = [[0]*N for _ in range(N)]
possible = True
for n in range(64):
    n = 2**n
    n1 = 0
    n2 = 0
    S_ = [False]*N
    T_ = [False]*N
    i1 = -1
    j1 = -1
    i2 = -1
    j2 = -1
    ud = {(0,0):0,(0,1):0,(1,0):0,(1,1):0}
    vd = {(0,0):0,(0,1):0,(1,0):0,(1,1):0}
    for i in range(N):
        s = S[i]
        u = U[i]&n
        if u:
            u = 1
        ud[(s,u)] += 1
    for j in range(N):
        t = T[j]
        v = V[j]&n
        if v:
            v = 1
        vd[(t,v)] += 1
    for i in range(N):
        s = S[i]
        u = U[i]&n
        if u>0:
            u = 1
        for j in range(N):
            t = T[j]
            v = V[j]&n
            if v>0:
                v = 1
            if u==v:
                ans[i][j] += n
            else:
                if (s,u)==(0,1) and (t,v)==(1,0):
                    possible = False
                    break
                if (s,u)==(1,0) and (t,v)==(0,1):
                    possible = False
                    break
                if (s,u)==(0,1) or (t,v)==(0,1):
                    ans[i][j] += n
    if ud[(0,0)]+ud[(1,0)]==N:
        if ud[(0,0)]<=1 and vd[(1,1)]==N:
            possible = False
        elif ud[(0,0)]==0 and vd[(1,1)]>0:
            possible = False
        elif vd[(1,1)]<N:
            for i in range(N):
                s = S[i]
                u = U[i]&n
                if u>0:
                    u = 1
                if (s,u)==(0,0):
                    for j in range(N):
                        t = T[j]
                        v = V[j]&n
                        if v:
                            v = 1
                        if (t,v)==(1,1):
                            ans[i][j] += n
        else:
            c = 0
            for i in range(N):
                s = S[i]
                u = U[i]&n
                if u>0:
                    u = 1
                if (s,u)==(0,0) and c==0:
                    for j in range(N):
                        t = T[j]
                        v = V[j]&n
                        if v:
                            v = 1
                        if (t,v)==(1,1):
                            if c==0:
                                c += 1
                            else:
                                ans[i][j] += n
                elif (s,u)==(0,0) and c==1:
                    for j in range(N):
                        t = T[j]
                        v = V[j]&n
                        if v:
                            v = 1
                        if (t,v)==(1,1):
                            ans[i][j] += n
                            c += 1
                            break
                if c==2:
                    break
    if vd[(0,0)]+vd[(1,0)]==N:
        if vd[(0,0)]<=1 and ud[(1,1)]==N:
            possible = False
        elif vd[(0,0)]==0 and ud[(1,1)]>0:
            possible = False
        elif ud[(1,1)]<N:
            for j in range(N):
                t = T[j]
                v = V[j]&n
                if v>0:
                    v = 1
                if (t,v)==(0,0):
                    for i in range(N):
                        s = S[i]
                        u = U[i]&n
                        if u:
                            u = 1
                        if (s,u)==(1,1):
                            ans[i][j] += n
        else:
            c = 0
            for j in range(N):
                t = T[j]
                v = V[j]&n
                if v>0:
                    v = 1
                if (s,v)==(0,0) and c==0:
                    for i in range(N):
                        s = S[i]
                        u = U[i]&n
                        if u:
                            u = 1
                        if (s,u)==(1,1):
                            if c==0:
                                c += 1
                            else:
                                ans[i][j] += n
                elif (t,v)==(0,0) and c==1:
                    for i in range(N):
                        s = S[i]
                        u = U[i]&n
                        if u:
                            u = 1
                        if (s,u)==(1,1):
                            ans[i][j] += n
                            c += 1
                            break
                if c==2:
                    break
            
                    

            
    if not possible:
        break

if possible:
    for i in range(N):
        print(*ans[i])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(map(int,input().split()))
T = list(map(int,input().split()))
U = list(map(int,input().split()))
V = list(map(int,input().split()))

ans = [[0]*N for _ in range(N)]
possible = True
for n in range(64):
    pn = 2**n
    ud = {(0,0):0,(0,1):0,(1,0):0,(1,1):0}
    vd = {(0,0):0,(0,1):0,(1,0):0,(1,1):0}
    for i in range(N):
        s = S[i]
        u = U[i]&pn
        if u:
            u = 1
        ud[(s,u)] += 1
    for j in range(N):
        t = T[j]
        v = V[j]&pn
        if v:
            v = 1
        vd[(t,v)] += 1
    if ud[(0,1)]*vd[(1,0)] or ud[(1,0)]*vd[(0,1)]:
        possible = False
        break
    if ud[(1,0)]+ud[(0,0)]==N:
        if ud[(0,0)]<=1 and vd[(1,1)]==N:
            possible = False
            break
        elif ud[(0,0)]==0 and vd[(1,1)]>=1:
            possible = False
            break
    if ud[(1,1)]+ud[(0,1)]==N:
        if ud[(1,1)]<=1 and vd[(0,0)]==N:
            possible = False
            break
        elif ud[(1,1)]==0 and vd[(0,0)]>=1:
            possible = False
            break
    if vd[(1,0)]+vd[(0,0)]==N:
        if vd[(0,0)]<=1 and ud[(1,1)]==N:
            possible = False
            break
        elif vd[(0,0)]==0 and ud[(1,1)]>=1:
            possible = False
            break
    if vd[(1,1)]+vd[(0,1)]==N:
        if vd[(1,1)]<=1 and ud[(0,0)]==N:
            possible = False
            break
        elif vd[(1,1)]==0 and ud[(0,0)]>=1:
            possible = False
            break
    for i in range(N):
        s = S[i]
        u = U[i]&pn
        if u:
            u = 1
        for j in range(N):
            t = T[j]
            v = V[j]&pn
            if v:
                v = 1
            if ud[(0,1)]*vd[(0,1)]:
                if (s,u) != (0,1) and (t,v) != (0,1):
                    ans[i][j] += pn
            elif ud[(1,0)]*vd[(1,0)]:
                if (s,u) == (0,1) or (t,v) == (0,1):
                    ans[i][j] += pn
            elif ud[(0,1)] or ud[(1,0)]:
                if u:
                    ans[i][j] += pn
            elif vd[(0,1)] or vd[(1,0)]:
                if v:
                    ans[i][j] += pn
    if ud[(0,0)]+ud[(1,0)]==N and vd[(1,1)]==N:
        c = 0
        for i in range(N):
            s = S[i]
            u = U[i]&pn
            if (s,u)==(0,0):
                if c==1:
                    ans[i][0] += pn
                    break
                if c==0:
                    for j in range(1,N):
                        ans[i][j] += pn
                    c+=1
    if ud[(0,0)]+ud[(1,0)]==N and vd[(1,1)]+vd[(0,0)]==N:
        for i in range(N):
            s = S[i]
            u = U[i]&pn
            if (s,u)==(0,0):
                for j in range(N):
                    t = T[j]
                    v = V[j]&pn
                    if v:
                        ans[i][j] += pn
                break
    if ud[(1,1)]+ud[(0,1)]==N and vd[(0,0)]==N:
        c = 0
        for i in range(N):
            s = S[i]
            u = U[i]&pn
            if (s,u)==(1,1):
                if c==1:
                    ans[i][0] -= pn
                    break
                if c==0:
                    for j in range(1,N):
                        ans[i][j] -= pn
                    c+=1
    if ud[(1,1)]+ud[(0,1)]==N and vd[(0,0)]+vd[(1,1)]==N:
        for i in range(N):
            s = S[i]
            u = U[i]&pn
            if (s,u)==(1,1):
                for j in range(N):
                    t = T[j]
                    v = V[j]&pn
                    if v:
                        ans[i][j] -= pn
                break
    if vd[(0,0)]+vd[(1,0)]==N and ud[(1,1)]==N:
        c = 0
        for j in range(N):
            t = T[j]
            v = V[j]&pn
            if (t,v)==(0,0):
                if c==1:
                    ans[0][j] += pn
                    break
                if c==0:
                    for i in range(1,N):
                        ans[i][j] += pn
                    c+=1
    if vd[(0,0)]+vd[(1,0)]==N and ud[(1,1)]+ud[(0,0)]==N:
        for j in range(N):
            t = T[j]
            v = V[j]&pn
            if (t,v)==(0,0):
                for i in range(N):
                    s = S[i]
                    u = U[i]&pn
                    if u:
                        ans[i][j] += pn
                break
    if vd[(1,1)]+vd[(0,1)]==N and ud[(0,0)]==N:
        c = 0
        for j in range(N):
            t = T[j]
            v = V[j]&pn
            if (t,v)==(1,1):
                if c==1:
                    ans[0][j] -= pn
                    break
                if c==0:
                    for i in range(1,N):
                        ans[i][j] -= pn
                    c+=1
    if vd[(1,1)]+vd[(0,1)]==N and ud[(0,0)]+ud[(1,1)]==N:
        for j in range(N):
            t = T[j]
            v = V[j]&pn
            if (t,v)==(1,1):
                for i in range(N):
                    s = S[i]
                    u = U[i]&pn
                    if u:
                        ans[i][j] -= pn
                break

if possible:
    for i in range(N):
        print(*ans[i])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from functools import reduce
from operator import and_, or_

N = int(input())
S, T, U, V = [[int(a) for a in input().split()] for _ in range(4)]
X = [[U[i] & V[j] if S[i] and T[j] else U[i] if T[j] else V[j] if S[i] else U[i] | V[j] for j in range(N)] for i in range(N)]
m = (1 << 64) - 1

for _ in range(2):
    A1 = [[m] * N for _ in range(N)]
    A2 = [[m] * N for _ in range(N)]
    for i in range(N-1)[::-1]:
        for j in range(N):
            A2[i][j] = A2[i+1][j] & X[i+1][j]

    for i in range(N):
        a = ~U[i] | reduce(or_, X[i])
        for j in range(N):
            if i:
                A1[i][j] = A1[i-1][j] & X[i-1][j]
            if S[i] and not T[j]:
                X[i][j] |= ~(A1[i][j] & A2[i][j] | a)
    
    X = [[X[j][i] for j in range(N)] for i in range(N)]
    S, T, U, V = T, S, V, U

if [reduce(or_ if S[i] else and_, x) for i, x in enumerate(X)] != U or [reduce(or_ if T[j] else and_, x) for j, x in enumerate(zip(*X))] != V:
    print(-1)
    exit()

for x in X:
    print(*x)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [int(a) for a in input().split()]
T = [int(a) for a in input().split()]
U = [int(a) for a in input().split()]
V = [int(a) for a in input().split()]
X = [[0] * N for _ in range(N)]
m = (1 << 64) - 1

for i in range(N):
    for j in range(N):
        if not S[i]:
            X[i][j] |= U[i]
        if not T[j]:
            X[i][j] |= V[j]
        X[i][j] |= U[i] & V[j]

A1 = [[m] * N for _ in range(N)]
A2 = [[m] * N for _ in range(N)]
for i in range(N-1)[::-1]:
    for j in range(N):
        A2[i][j] = A2[i+1][j] & X[i+1][j]

for i in range(N):
    a = 0
    for j in range(N):
        a |= X[i][j]
    for j in range(N):
        if i:
            A1[i][j] = A1[i-1][j] & X[i-1][j]
        if S[i] and not T[j]:
            X[i][j] |= U[i] & ~((A1[i][j] & A2[i][j]) | a)
        a |= X[i][j]
    if S[i] and a != U[i]:
        print(-1)
        exit()

A1 = [[m] * N for _ in range(N)]
A2 = [[m] * N for _ in range(N)]
for j in range(N-1)[::-1]:
    for i in range(N):
        A2[i][j] = A2[i][j+1] & X[i][j+1]

for j in range(N):
    a = 0
    for i in range(N):
        a |= X[i][j]
    for i in range(N):
        if j:
            A1[i][j] = A1[i][j-1] & X[i][j-1]
        if T[j] and not S[i]:
            X[i][j] |= V[j] & ~((A1[i][j] & A2[i][j]) | a)
        a |= X[i][j]
    if T[j] and a != V[j]:
        print(-1)
        exit()

for x in X:
    print(*x)