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: Runtime Error

Solution code:
import numpy as np

N = int(input())

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

ans = np.zeros((N,N),dtype = np.int64)
exit()
for n in range(64):
  u = (U>>n)&1
  v = (V>>n)&1
  invu = np.ones(N,int)-u
  invv = np.ones(N,int)-v
  b = np.zeros((N,N),int)
  check = np.zeros((N,N),int)
  ###行のAND=1とOR=0を埋める###
  for i in range(N):
    if S[i] != u[i]:
      if u[i] == 1:
        b[i] = np.ones(N,int)
      check[i] = np.ones(N,int)
  ###列のAND=1とOR=0を埋める###
  for i in range(N):
    if T[i] != v[i]:
      if v[i] == 1:
        if max(b[:,i]^check[:,i])!=0:
          print(-1)
          exit()
        b[:,i] = np.ones(N,int)
      if v[i] == 0:
        if max(b[:,i])!=0:
          print(-1)
          exit()
      check[:,i] = np.ones((1,N),int)
  ###行と列の値が一致するところを埋める###  
  same1 = np.dot(np.array([u]).T,np.ones((1,N),int)) & np.dot(np.array([v]).T,np.ones((1,N),int)).T
  same0 = np.dot(np.array([invu]).T,np.ones((1,N),int)) & np.dot(np.array([invv]).T,np.ones((1,N),int)).T
  b |= same1
  check |= (same1|same0)
  ###行のOR=1を満たしていく###  
  for i in range(N):
    if S[i] == u[i] == 1 and max(b[i]) == 0: #まだ満たされてない行を発見した場合
      flag = 0 #未解決
      for j in range(N):
        if T[j] == v[j] == b[i][j] == check[i][j] == 0 and sum(b[:,j]) < N-1:
          b[i][j] = check[i][j] = flag = 1
          break
      if not flag:
        print(-1)
        exit()
  
  ###列のOR=1を満たしていく### 
  for i in range(N):
    if T[i] == v[i] == 1 and max(b[:,i]) == 0: #まだ満たされてない列を発見した場合
      flag = 0 #未解決
      for j in range(N):
        if S[j] == u[j] == b[j][i] == check[j][i] == 0 and sum(b[j]) < N-1:
          b[j][i] = check[j][i] = flag = 1
          break
      if not flag:
        print(-1)
        exit()
  """
  for i in range(N):
    if S[i] == 0 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] == mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 0 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] != mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] != 0:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] == 0:
        print(-1)
        exit()
  
  for i in range(N):
    if T[i] == 0 and((v>>i) & 1) == 0:
      if b & mask_retu[i] == mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 0 and((v>>i) & 1) == 1:
      if b & mask_retu[i] != mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 0:
      if b & mask_retu[i] != 0:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 1:
      if b & mask_retu[i] == 0:
        print(-1)
        exit()
  """
  ans += b<<n


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

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())

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

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

for n in range(64):
  u = (U>>n)&1
  v = (V>>n)&1
  invu = np.ones(N,int)-u
  invv = np.ones(N,int)-v
  b = np.zeros((N,N),int)
  check = np.zeros((N,N),int)
  ###行のAND=1とOR=0を埋める###
  for i in range(N):
    if S[i] != u[i]:
      if u[i] == 1:
        b[i] = np.ones(N,int)
      check[i] = np.ones(N,int)
  ###列のAND=1とOR=0を埋める###
  for i in range(N):
    if T[i] != v[i]:
      if v[i] == 1:
        if max(b[:,i]^check[:,i])!=0:
          print(-1)
          exit()
        b[:,i] = np.ones(N,int)
      if v[i] == 0:
        if max(b[:,i])!=0:
          print(-1)
          exit()
      check[:,i] = np.ones((1,N),int)
  break
  ###行と列の値が一致するところを埋める###  
  same1 = np.dot(np.array([u]).T,np.ones((1,N),int)) & np.dot(np.array([v]).T,np.ones((1,N),int)).T
  same0 = np.dot(np.array([invu]).T,np.ones((1,N),int)) & np.dot(np.array([invv]).T,np.ones((1,N),int)).T
  b |= same1
  check |= (same1|same0)
  ###行のOR=1を満たしていく###  
  for i in range(N):
    if S[i] == u[i] == 1 and max(b[i]) == 0: #まだ満たされてない行を発見した場合
      flag = 0 #未解決
      for j in range(N):
        if T[j] == v[j] == b[i][j] == check[i][j] == 0 and sum(b[:,j]) < N-1:
          b[i][j] = check[i][j] = flag = 1
          break
      if not flag:
        print(-1)
        exit()
  
  ###列のOR=1を満たしていく### 
  for i in range(N):
    if T[i] == v[i] == 1 and max(b[:,i]) == 0: #まだ満たされてない列を発見した場合
      flag = 0 #未解決
      for j in range(N):
        if S[j] == u[j] == b[j][i] == check[j][i] == 0 and sum(b[j]) < N-1:
          b[j][i] = check[j][i] = flag = 1
          break
      if not flag:
        print(-1)
        exit()
  """
  for i in range(N):
    if S[i] == 0 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] == mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 0 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] != mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] != 0:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] == 0:
        print(-1)
        exit()
  
  for i in range(N):
    if T[i] == 0 and((v>>i) & 1) == 0:
      if b & mask_retu[i] == mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 0 and((v>>i) & 1) == 1:
      if b & mask_retu[i] != mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 0:
      if b & mask_retu[i] != 0:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 1:
      if b & mask_retu[i] == 0:
        print(-1)
        exit()
  """
  ans += b<<n


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

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())

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

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

for n in range(64):
  u = (U>>n)&1
  v = (V>>n)&1
  invu = np.ones(N,int)-u
  invv = np.ones(N,int)-v
  b = np.zeros((N,N),int)
  check = np.zeros((N,N),int)
  ###行のAND=1とOR=0を埋める###
  for i in range(N):
    if S[i] != u[i]:
      if u[i] == 1:
        b[i] = np.ones(N,int)
      check[i] = np.ones(N,int)
  ###列のAND=1とOR=0を埋める###
  for i in range(N):
    if T[i] != v[i]:
      if v[i] == 1:
        if max(b[:,i]^check[:,i])!=0:
          print(-1)
          exit()
        b[:,i] = np.ones(N,int)
      if v[i] == 0:
        if max(b[:,i])!=0:
          print(-1)
          exit()
      check[:,i] = np.ones((1,N),int)
  ###行と列の値が一致するところを埋める###  
  same1 = np.dot(np.array([u]).T,np.ones((1,N),int)) & np.dot(np.array([v]).T,np.ones((1,N),int)).T
  same0 = np.dot(np.array([invu]).T,np.ones((1,N),int)) & np.dot(np.array([invv]).T,np.ones((1,N),int)).T
  b |= same1
  check |= (same1|same0)
  ###行のOR=1を満たしていく###  
  for i in range(N):
    if S[i] == u[i] == 1 and max(b[i]) == 0: #まだ満たされてない行を発見した場合
      flag = 0 #未解決
      for j in range(N):
        if T[j] == v[j] == b[i][j] == check[i][j] == 0 and sum(b[:,j]) < N-1:
          b[i][j] = check[i][j] = flag = 1
          break
      if not flag:
        print(-1)
        exit()
  
  ###列のOR=1を満たしていく### 
  for i in range(N):
    if T[i] == v[i] == 1 and max(b[:,i]) == 0: #まだ満たされてない列を発見した場合
      flag = 0 #未解決
      for j in range(N):
        if S[j] == u[j] == b[j][i] == check[j][i] == 0 and sum(b[j]) < N-1:
          b[j][i] = check[j][i] = flag = 1
          break
      if not flag:
        print(-1)
        exit()
  """
  for i in range(N):
    if S[i] == 0 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] == mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 0 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] != mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] != 0:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] == 0:
        print(-1)
        exit()
  
  for i in range(N):
    if T[i] == 0 and((v>>i) & 1) == 0:
      if b & mask_retu[i] == mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 0 and((v>>i) & 1) == 1:
      if b & mask_retu[i] != mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 0:
      if b & mask_retu[i] != 0:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 1:
      if b & mask_retu[i] == 0:
        print(-1)
        exit()
  """
  ans += b<<n
  break

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

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())

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

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

for n in range(63):
  u = (U>>n)&1
  v = (V>>n)&1
  invu = np.ones(N,int)-u
  invv = np.ones(N,int)-v
  b = np.zeros((N,N),int)
  check = np.zeros((N,N),int)
  ###行のAND=1とOR=0を埋める###
  for i in range(N):
    if S[i] != u[i]:
      if u[i] == 1:
        b[i] = np.ones(N,int)
      check[i] = np.ones(N,int)
  ###列のAND=1とOR=0を埋める###
  for i in range(N):
    if T[i] != v[i]:
      if v[i] == 1:
        if max(b[:,i]^check[:,i])!=0:
          print(-1)
          exit()
        b[:,i] = np.ones(N,int)
      if v[i] == 0:
        if max(b[:,i])!=0:
          print(-1)
          exit()
      check[:,i] = np.ones((1,N),int)
  ###行と列の値が一致するところを埋める###  
  same1 = np.dot(np.array([u]).T,np.ones((1,N),int)) & np.dot(np.array([v]).T,np.ones((1,N),int)).T
  same0 = np.dot(np.array([invu]).T,np.ones((1,N),int)) & np.dot(np.array([invv]).T,np.ones((1,N),int)).T
  b |= same1
  check |= (same1|same0)
  ###行のOR=1を満たしていく###  
  for i in range(N):
    if S[i] == u[i] == 1 and max(b[i]) == 0: #まだ満たされてない行を発見した場合
      flag = 0 #未解決
      for j in range(N):
        if T[j] == v[j] == b[i][j] == check[i][j] == 0 and sum(b[:,j]) < N-1:
          b[i][j] = check[i][j] = flag = 1
          break
      if not flag:
        print(-1)
        exit()
  
  ###列のOR=1を満たしていく### 
  for i in range(N):
    if T[i] == v[i] == 1 and max(b[:,i]) == 0: #まだ満たされてない列を発見した場合
      flag = 0 #未解決
      for j in range(N):
        if S[j] == u[j] == b[j][i] == check[j][i] == 0 and sum(b[j]) < N-1:
          b[j][i] = check[j][i] = flag = 1
          break
      if not flag:
        print(-1)
        exit()
  """
  for i in range(N):
    if S[i] == 0 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] == mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 0 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] != mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] != 0:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] == 0:
        print(-1)
        exit()
  
  for i in range(N):
    if T[i] == 0 and((v>>i) & 1) == 0:
      if b & mask_retu[i] == mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 0 and((v>>i) & 1) == 1:
      if b & mask_retu[i] != mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 0:
      if b & mask_retu[i] != 0:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 1:
      if b & mask_retu[i] == 0:
        print(-1)
        exit()
  """
  ans += b<<n

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

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())

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

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

for n in range(64):
  u = (U>>n)&1
  v = (V>>n)&1
  invu = np.ones(N,int)-u
  invv = np.ones(N,int)-v
  b = np.zeros((N,N),int)
  check = np.zeros((N,N),int)
  ###行のAND=1とOR=0を埋める###
  for i in range(N):
    if S[i] != u[i]:
      if u[i] == 1:
        b[i] = np.ones(N,int)
      check[i] = np.ones(N,int)
  ###列のAND=1とOR=0を埋める###
  for i in range(N):
    if T[i] != v[i]:
      if v[i] == 1:
        if max(b[:,i]^check[:,i])!=0:
          print(-1)
          exit()
        b[:,i] = np.ones(N,int)
      if v[i] == 0:
        if max(b[:,i])!=0:
          print(-1)
          exit()
      check[:,i] = np.ones((1,N),int)
  ###行と列の値が一致するところを埋める###  
  same1 = np.dot(np.array([u]).T,np.ones((1,N),int)) & np.dot(np.array([v]).T,np.ones((1,N),int)).T
  same0 = np.dot(np.array([invu]).T,np.ones((1,N),int)) & np.dot(np.array([invv]).T,np.ones((1,N),int)).T
  b |= same1
  check |= (same1|same0)
  ###行のOR=1を満たしていく###  
  for i in range(N):
    if S[i] == u[i] == 1 and max(b[i]) == 0: #まだ満たされてない行を発見した場合
      flag = 0 #未解決
      for j in range(N):
        if T[j] == v[j] == b[i][j] == check[i][j] == 0 and sum(b[:,j]) < N-1:
          b[i][j] = check[i][j] = flag = 1
          break
      if not flag:
        print(-1)
        exit()
  
  ###列のOR=1を満たしていく### 
  for i in range(N):
    if T[i] == v[i] == 1 and max(b[:,i]) == 0: #まだ満たされてない列を発見した場合
      flag = 0 #未解決
      for j in range(N):
        if S[j] == u[j] == b[j][i] == check[j][i] == 0 and sum(b[j]) < N-1:
          b[j][i] = check[j][i] = flag = 1
          break
      if not flag:
        print(-1)
        exit()
  """
  for i in range(N):
    if S[i] == 0 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] == mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 0 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] != mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] != 0:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] == 0:
        print(-1)
        exit()
  
  for i in range(N):
    if T[i] == 0 and((v>>i) & 1) == 0:
      if b & mask_retu[i] == mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 0 and((v>>i) & 1) == 1:
      if b & mask_retu[i] != mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 0:
      if b & mask_retu[i] != 0:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 1:
      if b & mask_retu[i] == 0:
        print(-1)
        exit()
  """
  ans += b<<n

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

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())

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

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

for n in range(64):
  u = (U>>n)&1
  v = (V>>n)&1
  invu = np.ones(N,int)-u
  invv = np.ones(N,int)-v
  b = np.zeros((N,N),int)
  check = np.zeros((N,N),int)
  ###行のAND=1とOR=0を埋める###
  for i in range(N):
    if S[i] != u[i]:
      if u[i] == 1:
        b[i] = np.ones(N,int)
      check[i] = np.ones(N,int)
  ###列のAND=1とOR=0を埋める###
  for i in range(N):
    if T[i] != v[i]:
      if v[i] == 1:
        if max(b[:,i]^check[:,i])!=0:
          print(-1)
          exit()
        b[:,i] = np.ones(N,int)
      if v[i] == 0:
        if max(b[:,i])!=0:
          print(-1)
          exit()
      check[:,i] = np.ones((1,N),int)
  ###行と列の値が一致するところを埋める###  
  same1 = np.dot(np.array([u]).T,np.ones((1,N),int)) & np.dot(np.array([v]).T,np.ones((1,N),int)).T
  same0 = np.dot(np.array([invu]).T,np.ones((1,N),int)) & np.dot(np.array([invv]).T,np.ones((1,N),int)).T
  b |= same1
  check |= (same1|same0)
  ###行のOR=1を満たしていく###  
  for i in range(N):
    if S[i] == u[i] == 1 and max(b[i]) == 0: #まだ満たされてない行を発見した場合
      flag = 0 #未解決
      for j in range(N):
        if T[j] == v[j] == b[i][j] == check[i][j] == 0 and sum(b[:,j]) < N-1:
          b[i][j] = check[i][j] = flag = 1
          break
      if not flag:
        print(-1,233)
        exit()
  
  ###列のOR=1を満たしていく### 
  for i in range(N):
    if T[i] == v[i] == 1 and max(b[:,i]) == 0: #まだ満たされてない列を発見した場合
      flag = 0 #未解決
      for j in range(N):
        if S[j] == u[j] == b[j][i] == check[j][i] == 0 and sum(b[j]) < N-1:
          b[j][i] = check[j][i] = flag = 1
          break
      if not flag:
        print(-1)
        exit()
  """
  for i in range(N):
    if S[i] == 0 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] == mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 0 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] != mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 0:
      if b & mask_gyo[i] != 0:
        print(-1)
        exit()
    if S[i] == 1 and((u>>i) & 1) == 1:
      if b & mask_gyo[i] == 0:
        print(-1)
        exit()
  
  for i in range(N):
    if T[i] == 0 and((v>>i) & 1) == 0:
      if b & mask_retu[i] == mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 0 and((v>>i) & 1) == 1:
      if b & mask_retu[i] != mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 0:
      if b & mask_retu[i] != 0:
        print(-1)
        exit()
    if T[i] == 1 and((v>>i) & 1) == 1:
      if b & mask_retu[i] == 0:
        print(-1)
        exit()
  """
  ans += b<<n

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

Submission status: Runtime Error

Solution code:
import numpy as np
from numba import njit

@njit
def main(N,S,T,U,V):
    ans = np.zeros((N,N),dtype=np.int64)
    possible = True
    for n in range(64):
        n = 2**n
        n1 = 0
        n2 = 0
        S_ = np.zeros(N)
        s_ = 0
        T_ = np.zeros(N)
        t_ = 0
        i1 = -1
        j1 = -1
        i2 = -1
        j2 = -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 (s,u)==(0,1):
                    if (t,v)!=(1,0):
                        ans[i][j] += n
                    else:
                        possible = False
                        break
                elif (s,u)==(1,0):
                    if (t,v)==(0,1):
                        possible = False
                        break
                elif (t,v)==(0,1) or (t,v)==(1,0):
                    ans[i][j] += v*n
                elif (s,u)==(t,v):
                    ans[i][j] += u*n
                elif (s,u)==(0,0):
                    if n1==0:
                        i1 = i
                        j1 = j
                    else:
                        if i==i1 or j==j1:
                            ans[i][j] += n
                    n1 += 1
                elif (s,u)==(1,1):
                    if n2==0:
                        i2 = i
                        j2 = j
                    else:
                         if i==i2 or j==j2:
                             ans[i][j] += n
                    n2 += 1
                if u*n==ans[i][j]&n:
                    if not S_[i]:
                        s_ += 1
                    S_[i] += 1
                if v*n==ans[i][j]&n:
                    if not T_[j]:
                        t_ += 1
                    T_[j] += 1
        if s_!=N:
            ans[i2][j2] += n
            m = n
            for i in range(N):
                m &= ans[i][j2]
            if m&n:
                possible = False
        if t_!=N:
            ans[i1][j1] += n
            m = n
            for j in range(N):
                m &= ans[i1][j]
            if m&n:
                possible = False
        if not possible:
            break

    return possible,ans

if __name__ == '__main__':
    N = int(input())
    S = np.array(list(map(int,input().split())))
    T = np.array(list(map(int,input().split())))
    U = np.array(list(map(int,input().split())))
    V = np.array(list(map(int,input().split())))
    possible,ans = main(N,S,T,U,V)
    if possible:
        for i in range(N):
            print(*ans[i])
    else:
        print(-1)

Submission status: Runtime Error

Solution code:
from numba import njit
import numpy as np

@njit
def main(N,S,T,U,V):
    ans = [[0]*N for _ in range(N)]
    possible = True
    for n in range(64):
        n = 2**n
        n1 = 0
        n2 = 0
        S_ = np.zeros(N)
        T_ = np.zeros(N)
        i1 = -1
        j1 = -1
        i2 = -1
        j2 = -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 (s,u)==(0,1):
                    if (t,v)!=(1,0):
                        ans[i][j] += n
                    else:
                        possible = False
                        break
                elif (s,u)==(1,0):
                    if (t,v)==(0,1):
                        possible = False
                        break
                elif (t,v)==(0,1) or (t,v)==(1,0):
                    ans[i][j] += v*n
                elif (s,u)==(t,v):
                    ans[i][j] += u*n
                elif (s,u)==(0,0):
                    if n1==0:
                        i1 = i
                        j1 = j
                    else:
                        if i==i1 or j==j1:
                            ans[i][j] += n
                    n1 += 1
                elif (s,u)==(1,1):
                    if n2==0:
                        i2 = i
                        j2 = j
                    else:
                         if i==i2 or j==j2:
                             ans[i][j] += n
                    n2 += 1
                if u*n==ans[i][j]&n:
                    S_[i] = 1
                if v*n==ans[i][j]&n:
                    T_[j] = 1
        if np.sum(S_)!=N:
            ans[i2][j2] += n
            m = n
            for i in range(N):
                m &= ans[i][j2]
            if m&n:
                possible = False
        if np.sum(T_)!=N:
            ans[i1][j1] += n
            m = n
            for j in range(N):
                m &= ans[i1][j]
            if m&n:
                possible = False
        if not possible:
            break

    return possible,ans
    

if __name__ == '__main__':
    N = int(input())
    S = np.array(list(map(int,input().split())))
    T = np.array(list(map(int,input().split())))
    U = np.array(list(map(int,input().split())))
    V = np.array(list(map(int,input().split())))
    pos,ans = main(N,S,T,U,V)
    if pos:
        for i in range(N):
            print(*ans[i])
    else:
        print(-1)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
INF = float("inf")


def bit_n_most(v):
    if v == 0:
        return 0
    else:
        return len(bin(v))-2


def solve(N: int, S: "List[int]", T: "List[int]", U: "List[int]", V: "List[int]"):

    ans = [[0]*N for _ in range(N)]
    keta = 1
    for n in range(bit_n_most(max(max(U), max(V)))):
        # 下の桁から見ていく
        # 和が0であることが必要なもの、積が1であることが必要なものはいくつありますか
        good_rows = [False]*N
        good_cols = [False]*N
        undefined = []
        # 問題ないなら、構成していく
        # print("--")
        for i in range(N):
            for j in range(N):
                # (i, j)成分のn桁目(0-index)を決める。
                u = (U[i] >> n) & 1
                v = (V[j] >> n) & 1
                # print((u, v), end=" ")
                if ((S[i], u) == (0, 1) and (T[j], v) == (1, 0)) or\
                   ((S[i], u) == (1, 0) and (T[j], v) == (0, 1)):
                    # 実現出来ない組み合わせもある
                    print(-1)
                    return
                elif (S[i], u) == (0, 1) or (S[i], u) == (1, 0):
                    # 行の規定として、積が1 または 和が0 が決まっていれば、即決定する
                    ans[i][j] |= u << n
                    good_rows[i] = True
                elif (T[j], v) == (0, 1) or (T[j], v) == (1, 0):
                    # 列の規定として、積が1 または 和が0 が決まっていれば、即決定する
                    ans[i][j] |= v << n
                    good_cols[j] = True
                else:
                    # 和が1 積が0の組み合わせが到達した場合
                    if u == v:
                        # 行：(和が1)、列：（和が１） or
                        # 行：（積が0）、列：（積が0）の組。
                        # 決められる
                        ans[i][j] |= u << n
                        good_rows[i] = True
                        good_cols[j] = True
                    else:
                        if good_rows[i] == True:
                            # もし行がすでに制約を満たしているなら、行は無視して良い
                            ans[i][j] |= v << n
                            good_cols[j] = True
                        elif good_cols[j] == True:
                            # もし列がすでに制約を満たしているなら、列は無視して良い
                            ans[i][j] |= u << n
                            good_rows[i] = True
                        else:
                            # 困っちゃうので、あとで処理する
                            undefined.append((i, j))
        for i, j in undefined:
            sys.exit(1)
            # print(i, j)

        keta <<= 1

    # 手順でbit逆向きになってるので戻して出力な
    for i in range(N):
        print(*ans[i], sep=" ")
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    T = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    U = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    V = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, S, T, U, V)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

mask_gyo = [((1<<N)-1)<<(N*i) for i in range(N)]
mask_retu = [(sum(1<<(N*j) for j in range(N)))<<i for i in range(N)]

res = [[0 for i in range(N)] for j in range(N)]

for n in range(64):
  u = [(i>>n)&1 for i in U]
  v = [(i>>n)&1 for i in V]
  b = 0
  check = 0
  ###行のAND=1とOR=0を埋める###
  for i in range(N):
    if S[i] != u[i]:
      if u[i] == 1:
        b |= mask_gyo[i]
      check |= mask_gyo[i]
  ###列のAND=1とOR=0を埋める###
  for i in range(N):
    if T[i] != v[i]:
      if v[i] == 1:
        if b & mask_retu[i] != check & mask_retu[i]:
          print(-1)
          exit()
        b |= mask_retu[i]
      if v[i] == 0:
        if (b & mask_retu[i]) ^ (check & mask_retu[i]) != (check & mask_retu[i]):
          print(-1)
          exit()
      check |= mask_retu[i]
  ###行と列の値が一致するところを埋める###  
  for j in range(N):
    for i in range(N):
      if u[j] == v[i]:
        digit = (1<<i)<<(N*j)
        b |= digit*u[i]
        check |= digit
  ###行のOR=1を満たしていく###  
  for i in range(N):
    if S[i] == u[i] == 1:
      if b & mask_gyo[i] == 0: #まだ満たされてない行を発見した場合
        flag = 0 #未解決
        for j in range(N):
          digit = (1<<i)<<(N*j)
          if T[j]==1 and v[j]== 0 and check & digit == 0 and count_bit(b & mask_retu[j])<N-1:
            b |= digit
            check |= digit
            flag = 1 #解決
            break
        if not flag:
          print(-1)
          exit()
  ###列のOR=1を満たしていく###  
  for i in range(N):
    if T[i] == v[i] == 1:
      if b & mask_retu[i] == 0: #まだ満たされてない列を発見した場合
        flag = 0 #未解決
        for j in range(N):
          digit = (1<<j)<<(N*i)
          if S[j]==1 and u[j]== 0 and check & digit == 0 and count_bit(b & mask_gyo[j])<N-1:
            b |= digit
            check |= digit
            flag = 1 #解決
            break         
        if not flag:
          print(-1)
          exit()
  
  for j in range(N):
    for i in range(N):
      digit = (1<<j)<<(N*i)
      a = not (b & digit == 0)
      res[i][j] += a<<n

  
for item in res:
  print(*item)