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(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 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 a != V[j]:
        print(-1)
        exit()

for x in X:
    print(*x)

Submission status: Wrong Answer

Solution code:
def disp(s, L):
    return
    print(s + " =")
    for x in L:
        print(*x)
    print()

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]


disp("X", X)
###
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]
        
disp("X", X)
disp("A1", A1)
disp("A2", A2)
###
if False:
    for i in range(N):
        if not S[i]: continue
        A = [m] * N
        for j in range(N-1)[::-1]:
            A[j] = A[j+1] & X[i][j+1]
        a = m
        for j in range(N):
            if not T[j]:
                X[i][j] |= U[i] & ~(a & A[j])
            a &= X[i][j]
###
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] |= U[j] & ~(A1[i][j] & A2[i][j]) & ~a
        
        a |= X[i][j]


disp("X", X)
disp("A1", A1)
disp("A2", A2)
###
if False:
    for j in range(N):
        if not T[j]: continue
        A = [m] * N
        for i in range(N-1)[::-1]:
            A[i] = A[i+1] & X[i+1][j]
        a = m
        for i in range(N):
            if not S[i]:
                X[i][j] |= V[j] & ~(a & A[i])
            a &= X[i][j]
###
for i in range(N):
    if S[i] == 0:
        s = m
        for j in range(N):
            s &= X[i][j]
        if s != U[i]:
            print(-1)
            exit()
    else:
        s = 0
        for j in range(N):
            s |= X[i][j]
        if s != U[i]:
            print(-1)
            exit()

for j in range(N):
    if T[j] == 0:
        s = m
        for i in range(N):
            s &= X[i][j]
        if s != V[j]:
            print(-1)
            exit()
    else:
        s = 0
        for i in range(N):
            s |= X[i][j]
        if s != V[j]:
            print(-1)
            exit()

for x in X:
    print(*x)

disp("X", X)
disp("A1", A1)
disp("A2", A2)

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]

for i in range(N):
    if not S[i]: continue
    A = [m] * N
    for j in range(N-1)[::-1]:
        A[j] = A[j+1] & X[i][j+1]
    a = m
    for j in range(N):
        if not T[j]:
            X[i][j] |= U[i] & ~(a & A[j])
        a &= X[i][j]

for j in range(N):
    if not T[j]: continue
    A = [m] * N
    for i in range(N-1)[::-1]:
        A[i] = A[i+1] & X[i+1][j]
    a = m
    for i in range(N):
        if not S[i]:
            X[i][j] |= V[j] & ~(a & A[i])
        a &= X[i][j]

for i in range(N):
    if S[i] == 0:
        s = m
        for j in range(N):
            s &= X[i][j]
        if s != U[i]:
            print(-1)
            exit()
    else:
        s = 0
        for j in range(N):
            s |= X[i][j]
        if s != U[i]:
            print(-1)
            exit()

for j in range(N):
    if T[j] == 0:
        s = m
        for i in range(N):
            s &= X[i][j]
        if s != V[j]:
            print(-1)
            exit()
    else:
        s = 0
        for i in range(N):
            s |= X[i][j]
        if s != V[j]:
            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)]
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]

for i in range(N):
    if not S[i]: continue
    A = [-1] * N
    for j in range(N-1)[::-1]:
        A[j] = A[j+1] & X[i][j+1]
    a = -1
    for j in range(N):
        if not T[j]:
            X[i][j] |= U[i] & ~(a & A[j])
        a &= X[i][j]

for j in range(N):
    if not T[j]: continue
    A = [-1] * N
    for i in range(N-1)[::-1]:
        A[i] = A[i+1] & X[i+1][j]
    a = -1
    for i in range(N):
        if not S[i]:
            X[i][j] |= V[j] & ~(a & A[i])
        a &= X[i][j]

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)]
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]

for i in range(1, N):
    if not S[i]: continue
    A = [-1] * N
    for j in range(N-1)[::-1]:
        A[j] = A[j+1] & X[i][j+1]
    a = -1
    for j in range(N):
        if not T[j]:
            X[i][j] |= U[i] & ~(a & A[j])
        a &= X[i][j]

for j in range(1, N):
    if not T[j]: continue
    A = [-1] * N
    for i in range(N-1)[::-1]:
        A[i] = A[i+1] & X[i+1][j]
    a = -1
    for i in range(N):
        if not S[i]:
            X[i][j] |= V[j] & ~(a & A[i])
        a &= X[i][j]

for x in X:
    print(*x)

Submission status: Wrong Answer

Solution code:
def transpose(mat):
    mat = [list(x) for x in zip(*mat)]

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()))

mat = [[[-1]*64 for _ in range(n)] for _ in range(n)]
flag=True
s2 = [[0]*2 for _ in range(n)]
u2 = [[0]*2 for _ in range(n)]
for i in range(n):
    s2[i][0] = s[i]
    s2[i][1] = t[i]
    u2[i][0] = u[i]
    u2[i][1] = v[i]
ans = [[0]*n for _ in range(n)]
for k in range(64):
    mat = [[-1 for _ in range(n)] for _ in range(n)]
    for l in range(2):
        for i in range(n):
            x = u2[i][l] >> k & 1
            if s2[i][l] != x:
                for j in range(n):
                    if mat[i][j] != -1 and mat[i][j] != x:
                        flag=False
                        break
                    else:
                        mat[i][j] = x
        transpose(mat)


    inot = list()
    jnot = list()
    for i in range(n):
        filled = True
        for j in range(n):
            if mat[i][j] == -1:
                filled = False
        if not filled:
            inot.append(i)

    for j in range(n):
        filled = True
        for i in range(n):
            if mat[i][j] == -1:
                filled = False
        if not filled:
            jnot.append(j)

    if len(inot)>1 and len(jnot)>1:
        for i in range(len(inot)):
            for j in range(len(jnot)):
                mat[inot[i]][jnot[j]] = (i+j) %2
    elif len(inot)==1:
        transpose(mat)
        y = u2[i][0] >> k & 1
        l = 1
        for i in range(n):
            x = u2[i][l] >> k & 1
            if s2[i][l] == x:
                pending = list()
                ok = False
                for j in range(n):
                    if mat[i][j] == -1:
                        pending.append(j)
                    elif mat[i][j] == x:
                        ok = True
                if ok:
                    mat[i][j] = y
                    continue;
                if len(pending) == 0:
                    flag = False
                    break
                elif len(pending) == 1:
                    j = pending[0]
                    mat[i][j] = x
        transpose(mat)
        i = inot[0]
        ok = False
        for j in range(n):
            if mat[i][j] == y:
                ok = True
        if not ok:
            flag = False
            break
    elif len(jnot)==1:
        y = u2[i][1] >> k & 1
        l = 0
        for i in range(n):
            x = u2[i][l] >> k & 1
            if s2[i][l] == x:
                pending = list()
                ok = False
                for j in range(n):
                    if mat[i][j] == -1:
                        pending.append(j)
                    elif mat[i][j] == x:
                        ok = True
                if ok:
                    mat[i][j] = y
                    continue;
                if len(pending) == 0:
                    flag = False
                    break
                elif len(pending) == 1:
                    j = pending[0]
                    mat[i][j] = x
        j = jnot[0]
        ok = False
        for i in range(n):
            if mat[i][j] == y:
                ok = True
        if not ok:
            flag = False
            break  

    # add to ans
    if flag:
        for i in range(n):
            for j in range(n):
                    ans[i][j]+=mat[i][j]<<k
    else:
        print(-1)
        break

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()))

mat = [[[4]*64 for _ in range(n)] for _ in range(n)]
flag=True

exist1row =[[False]*64 for _ in range(n)]
exist1column = [[False]*64 for _ in range(n)]
exist0row = [[False]*64 for _ in range(n)]
exist0column = [[False]*64 for _ in range(n)]

sum1row=[list() for _ in range(64)]
sum1column = [list() for _ in range(64)]
for i in range(n):
    for j in range(n):
        for k in range(64):
            a = (u[i] & 1 << k) >> k
            b = (v[j] & 1 << k) >> k
            if s[i] and t[j]:
                if a == 0 or b == 0:
                    mat[i][j][k]=0
                    exist0row[i][k] =True
                    exist0column[j][k] =True
                else:
                    mat[i][j][k]=1
                    exist1row[i][k] =True
                    exist1column[j][k] =True
            elif s[i] == 0 and t[j] == 1:
                if a == 1 and b == 0:
                    flag=False
                elif a == 1:
                    mat[i][j][k]=1
                    exist1row[i][k] =True
                    exist1column[j][k] =True
                elif b == 0:
                    mat[i][j][k]=0
                    exist0row[i][k] =True
                    exist0column[j][k] =True
                else:
                    mat[i][j][k]=2
                    sum1column[k].append(j)

            elif s[i] == 1 and t[j] == 0:
                if a == 0 and b == 1:
                    flag=False
                elif a == 0:
                    mat[i][j][k]=0
                    exist0row[i][k] =True
                    exist0column[j][k] =True
                elif b == 1:
                    mat[i][j][k]=1
                    exist1row[i][k] =True
                    exist1column[j][k] =True
                else:
                    mat[i][j][k]=2
                    sum1row[k].append(i)

            else:
                if a == 1 or b == 1:
                    mat[i][j][k]=1
                    exist1row[i][k] =True
                    exist1column[j][k] =True
                else:
                    mat[i][j][k]=0
                    exist0row[i][k] =True
                    exist0column[j][k] =True


for k in range(64):
    for i in sum1row[k]:
        pending=list()
        for j in range(n):
            if mat[i][j][k]  == 2:
                if exist1row[i][k]:
                    mat[i][j][k]=0
                    exist0row[i][k] =True
                    exist0column[j][k] =True
                elif exist0column[j][k]:
                    mat[i][j][k]=1
                    exist1row[i][k] =True
                    exist1column[j][k] =True
                else:
                    mat[i][j][k]=1
                    exist1row[i][k] =True
                    exist1column[j][k] =True
    for j in sum1column[k]:
        pending=list()
        for i in range(n):
            if mat[i][j][k]  == 2:
                if exist1column[j][k]:
                    mat[i][j][k]=0
                    exist0row[i][k] =True
                    exist0column[j][k] =True
                elif exist0row[i][k]:
                    mat[i][j][k]=1
                    exist1row[i][k] =True
                    exist1column[j][k] =True
                else:
                    mat[i][j][k]=1
                    exist1row[i][k] =True
                    exist1column[j][k] =True


if flag:
    ans = [[0]*n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            for k in range(64):
                ans[i][j]+=mat[i][j][k]<<k

    for i in range(n):
        print(*ans[i])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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=[[[-1]*N for i in range(N)] for b in range(64)]

for b in range(64):
    for i in range(N):
        if S[i]==0 and (1<<b) & U[i]!=0:
            for j in range(N):
                if ANS[b][i][j]==0:
                    print(-1)
                    sys.exit()
                ANS[b][i][j]=1

        if S[i]==1 and (1<<b) & U[i]==0:
            for j in range(N):
                if ANS[b][i][j]==1:
                    print(-1)
                    sys.exit()
                ANS[b][i][j]=0


        if T[i]==0 and (1<<b) & V[i]!=0:
            for j in range(N):
                if ANS[b][j][i]==0:
                    print(-1)
                    sys.exit()
                ANS[b][j][i]=1

        if T[i]==1 and (1<<b) & V[i]==0:
            for j in range(N):
                if ANS[b][j][i]==1:
                    print(-1)
                    sys.exit()
                ANS[b][j][i]=0

for rep in range(2):
    for b in range(64):
        for i in range(N):
            if S[i]==0 and (1<<b)& U[i]==0:
                count_mi1=0
                okflag=0
                for j in range(N):
                    if ANS[b][i][j]==0:
                        okflag=1
                        break
                    if ANS[b][i][j]==-1:
                        count_mi1+=1
                    
                if okflag==0 and count_mi1==0:
                    print(-1)
                    sys.exit()

                if okflag==0 and count_mi1==1:
                    for j in range(N):
                        if ANS[b][i][j]==-1:
                            ANS[b][i][j]=0
                            break

            if S[i]==1 and (1<<b) & U[i]==1:
                count_mi1=0
                okflag=0
                for j in range(N):
                    if ANS[b][i][j]==1:
                        okflag=1
                        break
                    if ANS[b][i][j]==-1:
                        count_mi1+=1
                    
                if okflag==0 and count_mi1==0:
                    print(-1)
                    sys.exit()

                if okflag==0 and count_mi1==1:
                    for j in range(N):
                        if ANS[b][i][j]==-1:
                            ANS[b][i][j]=1
                            break

            if T[i]==0 and (1<<b)& V[i]==0:
                count_mi1=0
                okflag=0
                for j in range(N):
                    if ANS[b][j][i]==0:
                        okflag=1
                        break
                    if ANS[b][j][i]==-1:
                        count_mi1+=1
                    
                if okflag==0 and count_mi1==0:
                    print(-1)
                    sys.exit()

                if okflag==0 and count_mi1==1:
                    for j in range(N):
                        if ANS[b][j][i]==-1:
                            ANS[b][j][i]=0
                            break

            if T[i]==1 and (1<<b) & V[i]==1:
                count_mi1=0
                okflag=0
                for j in range(N):
                    if ANS[b][j][i]==1:
                        okflag=1
                        break
                    if ANS[b][j][i]==-1:
                        count_mi1+=1
                    
                if okflag==0 and count_mi1==0:
                    print(-1)
                    sys.exit()

                if okflag==0 and count_mi1==1:
                    for j in range(N):
                        if ANS[b][j][i]==-1:
                            ANS[b][j][i]=1
                            break

for b in range(64):
    for i in range(N):
        for j in range(N):
            if ANS[b][i][j]==-1:
                ANS[b][i][j]=(i+j)%2

for i in range(N):
    for j in range(N):
        S=""
        for b in range(63,-1,-1):
            S+=str(ANS[b][i][j])
        print(int(S,2),end=" ")
    print()

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)]
for k in range(64):
    tmp = [[-1]*N for _ in range(N)]
    for i,(s,u) in enumerate(zip(S,U)):
        b = (u>>k)&1
        if s==0 and b:
            for j in range(N):
                tmp[i][j] = 1
        if s and b==0:
            for j in range(N):
                tmp[i][j] = 0
    for j,(t,v) in enumerate(zip(T,V)):
        b = (v>>k)&1
        if t==0 and b:
            for i in range(N):
                if tmp[i][j] == 0:
                    print(-1)
                    exit()
                tmp[i][j] = 1
        if t and b==0:
            for i in range(N):
                if tmp[i][j] == 1:
                    print(-1)
                    exit()
                tmp[i][j] = 0
    row_1 = [0]*N
    col_1 = [0]*N
    for i,u in enumerate(U):
        for j,v in enumerate(V):
            if tmp[i][j] == 1:
                row_1[i] += 1
                col_1[j] += 1
                continue
            if tmp[i][j] == 0: continue
            ub,vb = (u>>k)&1, (v>>k)&1
            if ub != vb: continue
            if ub == 1:
                row_1[i] += 1
                col_1[j] += 1
            tmp[i][j] = ub

    for i,(s,u) in enumerate(zip(S,U)):
        ub = (u>>k)&1
        if not (s and ub): continue
        for j,(t,v) in enumerate(zip(T,V)):
            vb = (u>>k)&1
            if not (t==0 and vb==0): continue
            if col_1[j] >= N-1:
                print(-1)
                exit()
            row_1[i] += 1
            col_1[j] += 1
            tmp[i][j] = 1
    for j,(t,v) in enumerate(zip(T,V)):
        vb = (u>>k)&1
        if not (t and vb): continue
        for i,(s,u) in enumerate(zip(S,U)):
            ub = (u>>k)&1
            if not (s==0 and ub==0): continue
            if row_1[i] >= N-1:
                print(-1)
                exit()
            row_1[i] += 1
            col_1[j] += 1
            tmp[i][j] = 1
    for i in range(N):
        for j in range(N):
            t = tmp[i][j]
            if t == 1:
                ans[i][j] |= (1<<k)

for row in ans:
    print(*row)

Submission status: Wrong Answer

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  
  n = int(input())
  s = [[],[]]
  for i in range(2):
    s[i] = tuple(map(int,input().split()))
  u = [[],[]]
  for i in range(2):
    u[i] = tuple(map(int,input().split()))
  
  val = [[0 for _ in range(n)] for _ in range(2)]
  d = [[0 for _ in range(n)] for _ in range(n)]
  ans = [[0 for _ in range(n)] for _ in range(n)]
  
  def flip():
    for i in range(n):
      for j in range(i,n):
        d[i][j],d[j][i] = d[j][i],d[i][j]
    
  def solve():
    for i in range(n):
      for j in range(n):
        d[i][j] = -1
    for k in range(2):
      for i in range(n):
        if s[k][i] != val[k][i]:
          for j in range(n):
            if d[i][j] != -1 and d[i][j] != val[k][i]:
              return False
            d[i][j] = val[k][i]
      flip()
    #print(d)
    for l in range(1):
      for k in range(2):
        for i in range(n):
          if s[k][i] == val[k][i]:
            p = []
            ok = False
            for j in range(n):
              if d[i][j] == val[k][i]:
                ok = True
              if d[i][j] == -1:
                p.append(j)
            if ok:
              continue
            if len(p) == 0:
              return False
            elif len(p) == 1:
              d[i][p[0]] = val[k][i]
        flip()
    #print(d)
    start = 0
    for i in range(n):
      num = start
      flag = False
      for j in range(n):
        if d[i][j] == -1:
          flag = True
          d[i][j] = num
          num ^= 1
      if flag:
        start ^= 1
    return True
  
  for b in range(64):
    for i in range(2):
      for j in range(n):
        val[i][j] = (u[i][j]>>b)&1
    if solve():
      for i in range(n):
        for j in range(n):
          ans[i][j] |= d[i][j]<<b
    else:
      print(-1)
      exit()
  
  for i in range(n):
    print(*ans[i])

main()

Submission status: Wrong Answer

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N = ir()
    S = lr()
    T = lr()
    U = lr()
    V = lr()
    a = [[0]*N for i in range(N)]
    for k in range(64):
        val = [[bool(U[i]&(1<<k)), bool(V[i]&(1<<k))] for i in range(N)]
        d = [[-1]*N for i in range(N)]
        for i in range(N):
            x = val[i][0]
            if S[i] != x:
                for j in range(N):
                    if d[i][j] != x and d[i][j] != -1:
                        print(-1)
                        return
                    d[i][j] = x
        for j in range(N):
            x = val[j][1]
            if T[j] != x:
                for i in range(N):
                    if d[i][j] != x and d[i][j] != -1:
                        print(-1)
                        return
                    d[i][j] = x
        # for _ in range(2):
        for i in range(N):
            x = val[i][0]
            if S[i] == x:
                p = []
                ok = False
                for j in range(N):
                    if d[i][j] == -1:
                        p.append(j)
                    if d[i][j] == x:
                        ok = True
                if ok:
                    continue
                if len(p) == 0:
                    print(-1)
                    return
                if len(p) == 1:
                    d[i][p[0]] = x
        for j in range(N):
            x = val[j][1]
            if T[j] == x:
                p = []
                ok = False
                for i in range(N):
                    if d[i][j] == -1:
                        p.append(i)
                    if d[i][j] == x:
                        ok = True
                if ok:
                    continue
                if len(p) == 0:
                    print(-1)
                    return
                if len(p) == 1:
                    d[p[0]][j] = x
        ni = []
        nj = []
        for i in range(N):
            filled = True
            for j in range(N):
                if d[i][j] == -1:
                    filled = False
                    break
            if not filled:
                ni.append(i)
        for j in range(N):
            filled = True
            for i in range(N):
                if d[i][j] == -1:
                    filled = False
                    break
            if not filled:
                nj.append(j)
        for i, ii in enumerate(ni):
            for j, jj in enumerate(nj):
                d[ii][jj] = (i+j)%2
        for i in range(N):
            for j in range(N):
                a[i][j] |= d[i][j]<<k
    for i in range(N):
        print(*a[i], sep=' ')
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N = ir()
    S = lr()
    T = lr()
    U = lr()
    V = lr()
    # d = [[-1]*N for i in range(N)]
    # def flip():
    #     for i in range(N):
    #         for j in range(i):
    #             d[i][j], d[j][i] = d[j][i], d[i][j]
    a = [[0]*N for i in range(N)]
    for k in range(64):
        val = [[bool(U[i]&(1<<k)), bool(V[i]&(1<<k))] for i in range(N)]
        d = [[-1]*N for i in range(N)]
        # for b in range(2):
        for i in range(N):
            x = val[i][0]
            if S[i] != x:
                for j in range(N):
                    if d[i][j] != x and d[i][j] != -1:
                        print(-1)
                        return
                    d[i][j] = x
        for i in range(N):
            x = val[i][1]
            if T[i] != x:
                for j in range(N):
                    if d[j][i] != x and d[j][i] != -1:
                        print(-1)
                        return
                    d[j][i] = x
            # flip()
        for _ in range(2):
            # for b in range(2):
            for i in range(N):
                x = val[i][0]
                if S[i] == x:
                    p = []
                    ok = False
                    for j in range(N):
                        if d[i][j] == -1:
                            p.append(j)
                        if d[i][j] == x:
                            ok = True
                    if ok:
                        continue
                    if len(p) == 0:
                        print(-1)
                        return
                    if len(p) == 1:
                        d[i][p[0]] = x
            for i in range(N):
                x = val[i][1]
                if T[i] == x:
                    p = []
                    ok = False
                    for j in range(N):
                        if d[j][i] == -1:
                            p.append(i)
                        if d[j][i] == x:
                            ok = True
                    if ok:
                        continue
                    if len(p) == 0:
                        print(-1)
                        return
                    if len(p) == 1:
                        d[p[0]][i] = x
                # flip()
        ni = []
        nj = []
        for i in range(N):
            filled = True
            for j in range(N):
                if d[i][j] == -1:
                    filled = False
                    break
            if not filled:
                ni.append(i)
        for j in range(N):
            filled = True
            for i in range(N):
                if d[i][j] == -1:
                    filled = False
                    break
            if not filled:
                nj.append(j)
        for i, ii in enumerate(ni):
            for j, jj in enumerate(nj):
                d[ii][jj] = (i+j)%2
        for i in range(N):
            for j in range(N):
                a[i][j] |= d[i][j]<<k
    for i in range(N):
        print(*a[i], sep=' ')
resolve()