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: Accepted

Solution code:

"""

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

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

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

"""

from sys import stdin
import sys

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

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

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

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

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

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

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

    for j in range(N):

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

    #print (a)

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

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

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

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

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

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

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

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

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

        hi = h[0][0]

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

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

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

for i in ans:
    print (*i)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

S = list(map(int,input().split()))
T = list(map(int,input().split()))
U = np.array(list(map(int,input().split())),dtype = np.uint64)
V = np.array(list(map(int,input().split())),dtype = np.uint64)

ans = np.zeros((N,N),dtype = np.uint64)

for n in range(64):
  u = (U>>n)&1
  v = (V>>n)&1
  su = [int(S[i]*2+u[i]) for i in range(N)]
  tv = [int(T[i]*2+v[i]) for i in range(N)]
  suzerozero = []
  suoneone = []
  tvzerozero = []
  tvoneone = []
  for i in range(N):
    if su[i] == 0:
      suzerozero.append(i)
    if su[i] == 3:
      suoneone.append(i)
  for i in range(N):
    if tv[i] == 0:
      tvzerozero.append(i)
    if tv[i] == 3:
      tvoneone.append(i)
  
  b = np.zeros((N,N),dtype = np.uint64)
  ###行のAND=1とOR=0を埋める###
  for i in range(N):
    if su[i] == 1:
      b[i] = np.ones(N,int)
  ###列のAND=1とOR=0を埋める###
  for i in range(N):
    if tv[i] == 1:
      b[:,i] = np.ones(N,int)
  ###行と列の値が一致するところを埋める###  
  b |= np.dot(np.array([u]).T,np.ones((1,N),dtype = np.uint64)) & np.dot(np.array([v]).T,np.ones((1,N),dtype = np.uint64)).T
  ###行のOR=1を満たしていく### 
  if 1 not in tv and 3 not in tv:
    m = len(tvzerozero)
    now = 0
    for i in suoneone:
      b[i][tvzerozero[now%m]] += 1
      now += 1
  ###列のOR=1を満たしていく### 
  if 1 not in su and 3 not in su:
    m = len(suzerozero)
    now = 0
    for i in tvoneone:
      b[suzerozero[now%m]][i] += 1
      now += 1
  ans += b<<n

def end():
  print(-1)
  exit()
for i in range(N):
  if S[i] == 0:
    now = ans[i][0]
    for j in range(1,N):
      now &= ans[i][j]
  if S[i] == 1:
    now = ans[i][0]
    for j in range(1,N):
      now |= ans[i][j]
  if now != U[i]:
    end()
for i in range(N):
  if T[i] == 0:
    now = ans[0][i]
    for j in range(1,N):
      now &= ans[j][i]
  if T[i] == 1:
    now = ans[0][i]
    for j in range(1,N):
      now |= ans[j][i]
  if now != V[i]:
    end()

for item in ans:
  print(*list(item))

Submission status: Accepted

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**9)
    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()))

    range_N = range(N)
    ans = [[0]*N for _ in range_N]
    # 論理積
    for i in range_N:
        if S[i] == 0:
            for j in range_N:
                ans[i][j] = ans[i][j] | U[i]
        if T[i] == 0:   
            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 S[x] == 0:
            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 up == 0:
                break

    for y in range_N:
        if T[y] == 0:
            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 up == 0:
                break

    # チェック
    for i in range_N:
        check_xs = ans[i][0]
        check_ys = ans[0][i]
        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) \
        or (T[i] and V[i] != check_ys) \
        or (S[i] == 0 and U[i] != check_xm) \
        or (T[i] == 0 and V[i] != check_ym) :
            print(-1)
            return       

    for i in range_N:
        print(*ans[i])

main()

Submission status: Accepted

Solution code:
n,*s=map(int,open(0).read().split());s,t,f,g=[s[i*n:i*n+n]for i in range(4)];r=range(n);a=[n*[0]for _ in r];e=enumerate
for b in range(64):
 y=1<<b;u=[k&y for k in f];v=[k&y for k in g];l=[0]*n;m=l[:]
 for i,k in e(u):
  for j,h in e(v):
   if(k&h)|((s[i]^1)*k)|((t[j]^1)*h):a[i][j]|=y;l[i]+=1;m[j]+=1
 for i in r:
  if(l[i]==0)*s[i]*u[i]:
   for j in r:
    if(t[j]^1)*(n-m[j]>1):a[i][j]|=y;m[j]+=1;break
  if(m[i]==0)*t[i]*v[i]:
   for j in r:
    if(s[j]^1)*(n-l[j]>1):a[j][i]|=y;l[j]+=1;break
h=a[0][:];w=[i[0]for i in a]
for i,k in e(a):
 for j,l in e(k):w[i]=(l|w[i]if s[i]else w[i]&l);h[j]=(l|h[j]if t[j]else h[j]&l)
if g+f!=h+w:exit(print(-1))
for i in a:print(*i)

Submission status: Accepted

Solution code:
n,*s=map(int,open(0).read().split());s,t,f,g=[s[i*n:i*n+n]for i in range(4)];r=range(n);a=[n*[0]for _ in r];e=enumerate
for b in range(64):
 y=1<<b;u=[k&y for k in f];v=[k&y for k in g];l=[0]*n;m=l[:]
 for i,k in e(u):
  for j,h in e(v):
   if(k&h)|((s[i]^1)*k)|((t[j]^1)*h):a[i][j]|=y;l[i]+=1;m[j]+=1
 for i in r:
  if(l[i]==0)*s[i]*u[i]:
   for j in r:
    if(t[j]^1)*(n-m[j]>1):a[i][j]|=y;m[j]+=1;break
  if(m[i]==0)*t[i]*v[i]:
   for j in r:
    if(s[j]^1)*(n-l[j]>1):a[j][i]|=y;l[j]+=1;break
h=a[0][:];w=[i[0]for i in a]
for i,k in e(a):
 for j,l in e(k):w[i]=(l|w[i]if s[i]else w[i]&l);h[j]=(l|h[j]if t[j]else h[j]&l)
if(g!=h)|(f!=w):exit(print(-1))
for i in a:print(*i)

Submission status: Accepted

Solution code:
n=int(input())
s=list(map(int,input().split()))
t=list(map(int,input().split()))
u=list(map(lambda x:'0'*65+bin(int(x))[2:],input().split()))
v=list(map(lambda x:'0'*65+bin(int(x))[2:],input().split()))
ans=[[0]*n for _ in range(n)]
for i in range(65): # 2^iの位で考える
    ansi=[[-1]*n for _ in range(n)] # -1で残ったものは0とみなす
    for r in range(n): # 和で0、積で1の行と列に値を入れる。
        if s[r]==1 and u[r][-i-1]=='0':
            for c in range(n):
                ansi[r][c]=0
        elif s[r]==0 and u[r][-i-1]=='1':
            for c in range(n):
                ansi[r][c]=1
    for c in range(n):
        if t[c]==1 and v[c][-i-1]=='0':
            for r in range(n):
                if ansi[r][c]==-1:
                    ansi[r][c]=0
                elif ansi[r][c]==1:
                    print(-1)
                    exit(0)
        elif t[c]==0 and v[c][-i-1]=='1':
            for r in range(n):
                if ansi[r][c]==-1:
                    ansi[r][c]=1
                elif ansi[r][c]==0:
                    print(-1)
                    exit(0)
    for r in range(n):
        for c in range(n):
            if ansi[r][c]==-1 and u[r][-i-1]==v[c][-i-1]:
                ansi[r][c]=int(v[c][-i-1])


    for r in range(n): #行に対する操作
        if s[r]==1 and u[r][-i-1]=='1' and 1>max(ansi[r]): #和で1の行だが、まだ1がない
            ok=False
            for c in range(n):
                if ansi[r][c]<1 and t[c]==0 and v[c][-1-i]=='0' and 1>min([ansi[l][c] for l in range(n) if l!=r]): #積で0の列の0を1にする。ただし他の要素に0がある場合のみ。
                    ansi[r][c]=1
                    ok=True
                    break
            if ok==False:
                print(-1)
                exit(0)
        if s[r]==0 and u[r][-i-1]=='0' and 0<min(ansi[r]): #積で0の行だが、まだ0がない
            ok=False
            for c in range(n):
                if ansi[r][c]==1 and t[c]==1 and v[c][-1-i]=='1' and 0<max([ansi[l][c] for l in range(n) if l!=r]): #和で1の列の1を0にする。ただし他の要素に1がある場合のみ。
                    ansi[r][c]=0
                    ok=True
                    break
            if ok==False:
                print(-1)
                exit(0)
    for c in range(n): #列に対する操作
        if t[c]==1 and v[c][-i-1]=='1' and 1>max([ansi[l][c] for l in range(n)]): #和で1の列だが、まだ1がない
            ok=False
            for r in range(n):
                if ansi[r][c]<1 and s[r]==0 and u[r][-1-i]=='0' and 1>min([ansi[r][l] for l in range(n) if l!=c]): #積で0の行の0を1にする。ただし他の要素に0がある場合のみ。
                    ansi[r][c]=1
                    ok=True
                    break
            if ok==False:
                print(-1)
                exit(0)
        if t[c]==0 and v[c][-i-1]=='0' and 0<min([ansi[l][c] for l in range(n)]): #積で0の列だが、まだ0がない
            ok=False
            for r in range(n):
                if ansi[r][c]==1 and s[c]==1 and u[c][-1-i]=='1' and 0<max([ansi[r][l] for l in range(n) if l!=c]): #和で1の行の1を0にする。ただし他の要素に1がある場合のみ。
                    ansi[r][c]=1
                    ok=True
                    break
            if ok==False:
                print(-1)
                exit(0)
    for r in range(n):
        for c in range(n):
            ans[r][c]+=pow(2,i)*max(0,ansi[r][c])
for a in ans:
    print(' '.join(map(str,a)))

Submission status: Accepted

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

def invalid_exit():
    print(-1)
    exit()

ans = [[0]*N for _ in range(N)]
for k in range(64):
    tmp = [[-1]*N for _ in range(N)]
    row_has_0 = [0] * N
    col_has_0 = [0] * N
    row_has_1 = [0] * N
    col_has_1 = [0] * N
    def fill(i,j,bit):
        if bit == 1 - tmp[i][j]: invalid_exit()
        tmp[i][j] = bit
        if bit:
            row_has_1[i] = col_has_1[j] = 1
        else:
            row_has_0[i] = col_has_0[j] = 1
    blank_rows = set(range(N))
    blank_cols = set(range(N))
    for i,(s,u) in enumerate(zip(S,U)):
        x = (u>>k)&1
        if s==0 and x:
            for j in range(N):
                fill(i,j,1)
            blank_rows.remove(i)
        elif s and x==0:
            for j in range(N):
                fill(i,j,0)
            blank_rows.remove(i)
    for j,(t,v) in enumerate(zip(T,V)):
        x = (v>>k)&1
        if t==0 and x:
            for i in range(N):
                fill(i,j,1)
            blank_cols.remove(j)
        elif t and x==0:
            for i in range(N):
                fill(i,j,0)
            blank_cols.remove(j)

    if blank_rows or blank_cols:
        j = list(blank_cols)[0] if len(blank_cols)==1 else None
        for i,(s,u) in enumerate(zip(S,U)):
            x = (u>>k)&1
            if s==x:
                if s==0 and (not row_has_0[i]) and len(blank_cols) == 0: invalid_exit()
                if s==1 and (not row_has_1[i]) and len(blank_cols) == 0: invalid_exit()
                if j is not None:
                    y = (V[j]>>k)&1
                    if x==y:
                        fill(i,j,x)
                    else:
                        if x==0 and (not row_has_0[i]) and (not col_has_1[j]) and len(blank_rows) < 2: invalid_exit()
                        if x==1 and (not row_has_1[i]) and (not col_has_0[j]) and len(blank_rows) < 2: invalid_exit()
                        if row_has_0[i]:
                            fill(i,j,1)
                        else:
                            fill(i,j,0)
                    blank_rows.remove(i)
        if j is not None:
            blank_cols = set()

    if blank_rows or blank_cols:
        i = list(blank_rows)[0] if len(blank_rows)==1 else None
        for j,(t,v) in enumerate(zip(T,V)):
            x = (v>>k)&1
            if t==x:
                if t==0 and (not col_has_0[j]) and len(blank_rows) == 0: invalid_exit()
                if t==1 and (not col_has_1[j]) and len(blank_rows) == 0: invalid_exit()
                if i is not None:
                    y = (U[i]>>k)&1
                    if x==y:
                        fill(i,j,x)
                    else:
                        if x==0 and (not col_has_0[i]) and (not row_has_1[j]) and len(blank_cols) < 2: invalid_exit()
                        if x==1 and (not col_has_1[i]) and (not row_has_0[j]) and len(blank_cols) < 2: invalid_exit()
                        if col_has_0[i]:
                            fill(i,j,1)
                        else:
                            fill(i,j,0)
                    blank_cols.remove(j)
        if i is not None:
            blank_rows = set()

    assert len(blank_rows) != 1
    assert len(blank_cols) != 1
    for i,r in enumerate(blank_rows):
        for j,c in enumerate(blank_cols):
            tmp[r][c] = (i%2)^(j%2)
    for i,row in enumerate(tmp):
        for j,c in enumerate(row):
            if c == -1: invalid_exit()
            if c:
                ans[i][j] |= (1<<k)

for row in ans:
    print(*row)

Submission status: Accepted

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

def invalid_exit():
    print(-1)
    exit()

ans = [[0]*N for _ in range(N)]
for k in range(64):
    tmp = [[-1]*N for _ in range(N)]
    row_has_0 = [0] * N
    col_has_0 = [0] * N
    row_has_1 = [0] * N
    col_has_1 = [0] * N
    def fill(i,j,bit):
        if bit == 1 - tmp[i][j]: invalid_exit()
        tmp[i][j] = bit
        if bit:
            row_has_1[i] = col_has_1[j] = 1
        else:
            row_has_0[i] = col_has_0[j] = 1
    blank_rows = set(range(N))
    blank_cols = set(range(N))
    for i,(s,u) in enumerate(zip(S,U)):
        x = (u>>k)&1
        if s==0 and x:
            for j in range(N):
                tmp[i][j] = 1
                col_has_1[j] = 1
            row_has_1[i] = 1
            blank_rows.remove(i)
        elif s and x==0:
            for j in range(N):
                tmp[i][j] = 0
                col_has_0[j] = 1
            row_has_0[i] = 1
            blank_rows.remove(i)
    for j,(t,v) in enumerate(zip(T,V)):
        x = (v>>k)&1
        if t==0 and x:
            for i in range(N):
                if tmp[i][j] == 0: invalid_exit()
                tmp[i][j] = 1
                row_has_1[i] = 1
            col_has_1[j] = 1
            blank_cols.remove(j)
        elif t and x==0:
            for i in range(N):
                if tmp[i][j] == 1: invalid_exit()
                tmp[i][j] = 0
                row_has_0[i] = 1
            col_has_0[j] = 1
            blank_cols.remove(j)

    if blank_rows or blank_cols:
        j = list(blank_cols)[0] if len(blank_cols)==1 else None
        for i,(s,u) in enumerate(zip(S,U)):
            x = (u>>k)&1
            if s==x:
                if s==0 and (not row_has_0[i]) and len(blank_cols) == 0: invalid_exit()
                if s==1 and (not row_has_1[i]) and len(blank_cols) == 0: invalid_exit()
                if j is not None:
                    y = (V[j]>>k)&1
                    if x==y:
                        fill(i,j,x)
                    else:
                        if x==0 and (not row_has_0[i]) and (not col_has_1[j]) and len(blank_rows) < 2: invalid_exit()
                        if x==1 and (not row_has_1[i]) and (not col_has_0[j]) and len(blank_rows) < 2: invalid_exit()
                        if row_has_0[i]:
                            fill(i,j,1)
                        else:
                            fill(i,j,0)
                    blank_rows.remove(i)
        if j is not None:
            blank_cols = set()

    if blank_rows or blank_cols:
        i = list(blank_rows)[0] if len(blank_rows)==1 else None
        for j,(t,v) in enumerate(zip(T,V)):
            x = (v>>k)&1
            if t==x:
                if t==0 and (not col_has_0[j]) and len(blank_rows) == 0: invalid_exit()
                if t==1 and (not col_has_1[j]) and len(blank_rows) == 0: invalid_exit()
                if i is not None:
                    y = (U[i]>>k)&1
                    if x==y:
                        fill(i,j,x)
                    else:
                        if x==0 and (not col_has_0[i]) and (not row_has_1[j]) and len(blank_cols) < 2: invalid_exit()
                        if x==1 and (not col_has_1[i]) and (not row_has_0[j]) and len(blank_cols) < 2: invalid_exit()
                        if col_has_0[i]:
                            fill(i,j,1)
                        else:
                            fill(i,j,0)
                    blank_cols.remove(j)
        if i is not None:
            blank_rows = set()

    assert len(blank_rows) != 1
    assert len(blank_cols) != 1
    for i,r in enumerate(blank_rows):
        for j,c in enumerate(blank_cols):
            tmp[r][c] = (i%2)^(j%2)
    for i,row in enumerate(tmp):
        for j,c in enumerate(row):
            if c == -1: invalid_exit()
            if c:
                ans[i][j] |= (1<<k)

for row in ans:
    print(*row)

Submission status: Accepted

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

def invalid_exit():
    print(-1)
    exit()

ans = [[0]*N for _ in range(N)]
for k in range(64):
    tmp = [[-1]*N for _ in range(N)]
    row_has_0 = [0] * N
    col_has_0 = [0] * N
    row_has_1 = [0] * N
    col_has_1 = [0] * N
    def fill(i,j,bit):
        if bit == 1 - tmp[i][j]: invalid_exit()
        tmp[i][j] = bit
        if bit:
            row_has_1[i] = col_has_1[j] = 1
        else:
            row_has_0[i] = col_has_0[j] = 1
    blank_rows = set(range(N))
    blank_cols = set(range(N))
    for i,(s,u) in enumerate(zip(S,U)):
        x = (u>>k)&1
        if s==0 and x:
            for j in range(N):
                tmp[i][j] = 1
                col_has_1[j] = 1
            row_has_1[i] = 1
            blank_rows.remove(i)
        elif s and x==0:
            for j in range(N):
                tmp[i][j] = 0
                col_has_0[j] = 1
            row_has_0[i] = 1
            blank_rows.remove(i)
    for j,(t,v) in enumerate(zip(T,V)):
        x = (v>>k)&1
        if t==0 and x:
            for i in range(N):
                if tmp[i][j] == 0: invalid_exit()
                tmp[i][j] = 1
                row_has_1[i] = 1
            col_has_1[j] = 1
            blank_cols.remove(j)
        elif t and x==0:
            for i in range(N):
                if tmp[i][j] == 1: invalid_exit()
                tmp[i][j] = 0
                row_has_0[i] = 1
            col_has_0[j] = 1
            blank_cols.remove(j)

    for _ in range(2):
        if blank_rows or blank_cols:
            j = list(blank_cols)[0] if len(blank_cols)==1 else None
            for i,(s,u) in enumerate(zip(S,U)):
                x = (u>>k)&1
                if s==x:
                    if s==0 and (not row_has_0[i]) and len(blank_cols) == 0: invalid_exit()
                    if s==1 and (not row_has_1[i]) and len(blank_cols) == 0: invalid_exit()
                    if j is not None:
                        y = (V[j]>>k)&1
                        if x==y:
                            fill(i,j,x)
                        else:
                            if x==0 and (not row_has_0[i]) and (not col_has_1[j]) and len(blank_rows) < 2: invalid_exit()
                            if x==1 and (not row_has_1[i]) and (not col_has_0[j]) and len(blank_rows) < 2: invalid_exit()
                            if row_has_0[i]:
                                fill(i,j,1)
                            else:
                                fill(i,j,0)
                        blank_rows.remove(i)
            if j is not None:
                blank_cols = set()

        if blank_rows or blank_cols:
            i = list(blank_rows)[0] if len(blank_rows)==1 else None
            for j,(t,v) in enumerate(zip(T,V)):
                x = (v>>k)&1
                if t==x:
                    if t==0 and (not col_has_0[j]) and len(blank_rows) == 0: invalid_exit()
                    if t==1 and (not col_has_1[j]) and len(blank_rows) == 0: invalid_exit()
                    if i is not None:
                        y = (U[i]>>k)&1
                        if x==y:
                            fill(i,j,x)
                        else:
                            if x==0 and (not col_has_0[i]) and (not row_has_1[j]) and len(blank_cols) < 2: invalid_exit()
                            if x==1 and (not col_has_1[i]) and (not row_has_0[j]) and len(blank_cols) < 2: invalid_exit()
                            if col_has_0[i]:
                                fill(i,j,1)
                            else:
                                fill(i,j,0)
                        blank_cols.remove(j)
            if i is not None:
                blank_rows = set()

    assert len(blank_rows) != 1
    assert len(blank_cols) != 1
    for i,r in enumerate(blank_rows):
        for j,c in enumerate(blank_cols):
            tmp[r][c] = (i%2)^(j%2)
    for i,row in enumerate(tmp):
        for j,c in enumerate(row):
            if c == -1: invalid_exit()
            if c:
                ans[i][j] |= (1<<k)

for row in ans:
    print(*row)