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=list(map(int,input().split()))
t=list(map(int,input().split()))
u=list(map(int,input().split()))
v=list(map(int,input().split()))
ans=[n*[0]for _ in range(n)]
ss=[u[i]if s[i]==0 else 0 for i in range(n)]
tt=[v[i]if t[i]==0 else 0 for i in range(n)]
son=[[]for _ in range(n)]
ton=[[]for _ in range(n)]
sno=[[n-1]*64 for _ in range(n)]
tno=[[n-1]*64 for _ in range(n)]
for i in range(n):
  for j in range(n):
    ans[i][j]=u[i]&v[j]
    if s[i]==0:
      ans[i][j]|=u[i]
      ss[i]&=ans[i][j]
    else:
      ss[i]|=ans[i][j]
    if t[j]==0:
      ans[i][j]|=v[j]
      tt[j]&=ans[i][j]
    else:
      tt[j]|=ans[i][j]
    if s[i]==0 or t[i]==0:
      for k in range(64):
        if ans[i][j]&1<<k:
          if s[i]==0:sno[i][k]-=1
          if t[j]==0:tno[j][k]-=1
    if j==n-1:
      if s[i]==1:
        if ss[i]|u[i]>u[i]:print(-1);exit()
        if ss[i]!=u[i]:
          for k in range(64):
            if u[i]&1<<k:
              if (ss[i]&1<<k)==0:
                son[i].append(k)
          ss[i]=1
        else:ss[i]=0
      else:
        if ss[i]!=u[i]:print(-1);exit()
        else:ss[i]=2
    if i==n-1:
      if t[j]==1:
        if tt[j]|v[j]>v[j]:print(-1);exit()
        if tt[j]!=v[j]:
          for k in range(64):
            if v[j]&1<<k:
              if (tt[j]&1<<k)==0:
                ton[j].append(k)
          tt[j]=1
        else:tt[j]=0
      else:
        if tt[j]!=v[j]:print(-1);exit()
        else:tt[j]=2
for i in range(n):
  for k in son[i]:
    for j in range(n):
      if t[j]==0:
        if tno[j][k]>0:
          ans[i][j]|=1<<k
          tno[j][k]-=1
          break
      if j==n-1:print(-1);exit()
for j in range(n):
  for k in ton[j]:
    for i in range(n):
      if s[i]==0:
        if sno[i][k]>0:
          ans[i][j]|=1<<k
          sno[i][k]-=1
          break
      if i==n-1:print(-1);exit()
for i in ans:print(*i)

Submission status: Wrong Answer

Solution code:
import heapq

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()))
total = [[0]*n for i in range(n)]

works = True
for bit in range(64):
    if not works:
        break
    
    out = [[-1]*n for i in range(n)]
    qInit = []

    done = [False] * (4 * n)
    
    for i in range(n):
        if s[i] != (u[i] & 1):
            for j in range(n):
                if out[i][j] == s[i]:
                    works = False
                    break
                out[i][j] = u[i] & 1
                done[4 * i + 0 + (u[i] & 1)] = True
                done[4 * j + 2 + (u[i] & 1)] = True
        else:
            qInit.append((i,0,s[i]))

    for j in range(n):
        if t[j] != (v[j] & 1):  
            for i in range(n):
                if out[i][j] == t[j]:
                    works = False
                    break
                out[i][j] = v[j] & 1
                done[4 * i + 0 + (v[j] & 1)] = True
                done[4 * j + 2 + (v[j] & 1)] = True
        else:
            qInit.append((j,1,t[j]))

    counts = dict()

    q = []
    for i, d, b in qInit:
        if not done[4 * i + 2 * d + b]:
            if d == 0:
                for j in range(n):
                    count = 0
                    if out[i][j] == -1:
                        count += 1
                heapq.heappush(q, (count,i,d,b))
                counts[(i,d,b)] = count
            else:
                for j in range(n):
                    count = 0
                    if out[j][i] == -1:
                        count += 1
                heapq.heappush(q, (count,i,d,b))
                counts[(i,d,b)] = count
                    
                
    while q:
        count, i, d, b = heapq.heappop(q)
        if not done[4 * i + 2 * d + b]:
            if count == 0:
                works = False
                break
            if d == 0:
                for j in range(n):
                    if out[i][j] == -1:
                        break
                    
                assert(out[i][j] == -1)
                out[i][j] = b
                #done.add((i,0,b))
                done[4 * i + 2 * 0 + b] = True
                #done.add((j,1,b))
                done[4 * j + 2 * 1 + b] = True
                if (i,0,1-b) in counts:
                    counts[(i,0,1-b)] -= 1
                    heapq.heappush(q, (counts[(i,0,1-b)], i, 0, 1 - b))
                if (j,1,1-b) in counts:
                    counts[(j,1,1-b)] -= 1
                    heapq.heappush(q, (counts[(j,1,1-b)], j, 1, 1 - b))
            else:
                j = i
                for i in range(n):
                    if out[i][j] == -1:
                        break
                assert(out[i][j] == -1)
                
                out[i][j] = b
                #done.add((i,0,b))
                done[4 * i + 2 * 0 + b] = True
                #done.add((j,1,b))
                done[4 * j + 2 * 1 + b] = True
                if (i,0,1-b) in counts:
                    counts[(i,0,1-b)] -= 1
                    heapq.heappush(q, (counts[(i,0,1-b)], i, 0, 1 - b))
                if (j,1,1-b) in counts:
                    counts[(j,1,1-b)] -= 1
                    heapq.heappush(q, (counts[(j,1,1-b)], j, 1, 1 - b))

    #Clean-Up
    mask = 1 << bit
    for i in range(n):
        for j in range(n):
            if out[i][j] == 1:
                total[i][j] |= mask

    for i in range(n):
        u[i] >>= 1
        v[i] >>= 1
    
if works:
    totalS = ['']*n
    for i in range(n):
        totalS[i] = ' '.join(map(str,total[i]))
    print(*totalS,sep='\n')
else:
    print(-1)

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()]
rand, ror, cand, cor = 0, 0, 0, 0
RT, RN, RX, CT, CN, CX = [], [], [], [], [], []
for i in range(N):
    if S[i]:
        RT.append(1)
        RN.append(ror)
        RX.append(U[i])
        ror += 1
    else:
        RT.append(0)
        RN.append(rand)
        RX.append(U[i])
        rand += 1
    if T[i]:
        CT.append(1)
        CN.append(cor)
        CX.append(V[i])
        cor += 1
    else:
        CT.append(0)
        CN.append(cand)
        CX.append(V[i])
        cand += 1

X = [[0] * N for _ in range(N)]
ROK = [0] * N
COK = [0] * N
for r in range(N):
    for c in range(N):
        if RT[r] == 0 and CT[c] == 0:
            X[r][c] = RX[r] | CX[c]
            if X[r][c] == RX[r]: ROK[r] = 1
            if X[r][c] == CX[c]: COK[c] = 1
        elif RT[r] == 1 and CT[c] == 1:
            X[r][c] = RX[r] & CX[c]
            if X[r][c] == RX[r]: ROK[r] = 1
            if X[r][c] == CX[c]: COK[c] = 1
        elif RT[r] == 0 and CT[c] == 1:
            if RX[r] & CX[c] != RX[r]:
                print(-1)
                exit()
            elif RX[r] == CX[c]:
                ROK[r] = 1
                COK[c] = 1
                X[r][c] = RX[r]
            else:
                X[r][c] = -1
        elif RT[r] == 1 and CT[c] == 0:
            if RX[r] & CX[c] != CX[c]:
                print(-1)
                exit()
            elif RX[r] == CX[c]:
                ROK[r] = 1
                COK[c] = 1
                X[r][c] = RX[r]
            else:
                X[r][c] = -1
        else:
            print(1 // 0)

for r in range(N):
    for c in range(N):
        if X[r][c] < 0:
            if RT[r] == 0 and CT[c] == 1:
                if rand <= cor:
                    X[r][c] = CX[c] if (RN[r] == CN[c] or CN[c] >= rand) and (COK[c] == 0) else RX[r]
                else:
                    X[r][c] = RX[r] if (RN[r] == CN[c] or RN[r] >= cor) and (ROK[r] == 0) else CX[c]
            else:
                if cand <= ror:
                    X[r][c] = RX[r] if (RN[r] == CN[c] or RN[r] >= cand) and (ROK[r] == 0) else CX[c]
                else:
                    X[r][c] = CX[c] if (RN[r] == CN[c] or CN[c] >= ror) and (COK[c] == 0) else RX[r]

for r in range(N):
    if RT[r] == 0:
        s = (1 << 64) - 1
        for c in range(N):
            s = s & X[r][c]
        if s != RX[r]:
            print(-1)
            exit()
    if RT[r] == 1:
        s = 0
        for c in range(N):
            s = s | X[r][c]
        if s != RX[r]:
            print(-1)
            exit()

for c in range(N):
    if CT[c] == 0:
        s = (1 << 64) - 1
        for r in range(N):
            s = s & X[r][c]
        if s != CX[c]:
            print(-1)
            exit()
    if CT[c] == 1:
        s = 0
        for r in range(N):
            s = s | X[r][c]
        if s != CX[c]:
            print(-1)
            exit()

for x in X:
    print(*x)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np

N = int(readline())
S = np.array(readline().split(), np.uint64)
T = np.array(readline().split(), np.uint64)
U = np.array(readline().split(), np.uint64)
V = np.array(readline().split(), np.uint64)


def solve(S, T, U, V):
    A = np.zeros((N, N), np.uint64)
    if N == 1:
        return np.array([[U[0]]], np.uint64)
    all_one_row = (S == 0) & (U == 1)
    all_zero_row = (S == 1) & (U == 0)
    r = np.any(all_zero_row), np.any(all_one_row)
    all_one_col = (T == 0) & (V == 1)
    all_zero_col = (T == 1) & (V == 0)
    c = np.any(all_zero_col), np.any(all_one_col)
    if (c[0] or c[1]) and not (r[0] or r[1]):
        return solve(T, S, V, U).T
    # 確定条件があるなら行にある
    if not (r[0] or r[1]):
        # 確定条件なし。十分均一ならok
        A[1::2, ::2] = 1
        A[::2, 1::2] = 1
        return A
    # 少なくとも行に確定条件がある場合
    if r[0] and r[1]:
        # 列の運命は確定なので、行本位で入れればよい
        A += U[:, None]
        return A
    if r[0]:
        if c[1]:
            return A
        if c[0]:
            # 行列とも強制的に 0 が入る。他は 1 を埋める。
            A += 1
            A[all_zero_row, :] = 0
            A[:, all_zero_col] = 0
            return A
        # 列に強制条件なし
        if np.any(U == 1):
            A += U[:, None]
            return A
        # 行は、all or exists で 0
        # 列は、exists 0 or 1
        if np.any(V == 0):
            A += V[None, :]
            A[all_zero_row] = 0
            return A
        if N >= np.count_nonzero(all_zero_row) - 1:
            return A
        ind = np.where(~all_zero_row)[0]
        A[ind[0], 0] = 1
        A[ind[-1], 1:] = 1
        return A
    if r[1]:
        if c[0]:
            return A
        if c[1]:
            # 行列とも強制的に 1 が入る。他は 0 を埋める。
            A[all_one_row, :] = 1
            A[:, all_one_col] = 1
            return A
        # 列に強制条件なし
        if np.any(U == 0):
            A += U[:, None]
            return A
        # 行は、all or exists で 1
        # 列は、exists 0 or 1
        if np.any(V == 1):
            A += V[None, :]
            A[all_one_row] = 1
            return A
        if N >= np.count_nonzero(all_one_row) - 1:
            return A
        A += 1
        ind = np.where(~all_one_row)[0]
        A[ind[0], 0] = 0
        A[ind[-1], 1:] = 0
        return A


def test(S, T, U, V, A):
    row_or = np.bitwise_or.reduce(A, axis=1)
    row_and = np.bitwise_and.reduce(A, axis=1)
    cond_row = np.all(U[S == 0] == row_and[S == 0]) and np.all(U[S == 1] == row_or[S == 1])
    col_or = np.bitwise_or.reduce(A, axis=0)
    col_and = np.bitwise_and.reduce(A, axis=0)
    cond_col = np.all(V[T == 0] == col_and[T == 0]) and np.all(V[T == 1] == col_or[T == 1])
    return cond_row and cond_col


A = np.zeros((N, N), np.uint64)
for k in range(64):
    A ^= solve(S, T, U >> k, V >> k) << k

if not test(S, T, U, V, A):
    print(-1)
else:
    print(A)

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()))
matrix=[[0 for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        if s[i]==0 and t[j]==0:
            matrix[i][j]=u[i]|v[j]
        elif s[i]==0 and t[j]==1:
            if u[i]|v[j]!=v[j]:print(-1);exit()
            matrix[i][j]=u[i]
        elif s[i]==1 and t[j]==0:
            if u[i]|v[j]!=u[i]:print(-1);exit()
            matrix[i][j]=v[j]
        else:
            matrix[i][j]=u[i] & v[j]
for i in matrix:print(*i)

Submission status: Wrong Answer

Solution code:
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


def f(S, T):
    N = len(S)
    F = [[0 for _ in range(N)] for __ in range(N)]
    
    for i in range(N):
        for j in range(N):
            SS = S[i]
            TT = T[j]
            if TT == (0, 0):
                F[i][j] = 1 if SS == (0, 1) else 0
            elif TT == (0, 1):
                if SS == (1, 0):
                    return None
                F[i][j] = 1
            elif TT == (1, 0):
                if SS == (0, 1):
                    return None
                F[i][j] = 0
            else:
                F[i][j] = 0 if SS == (1, 0) else 1

    return F


def main():
    N = int(input())

    S = read_list()
    T = read_list()
    max_bit_len = 4
    U = tuple(map(lambda l: format(l, "0" + str(max_bit_len) + "b"), read_list()))
    V = tuple(map(lambda l: format(l, "0" + str(max_bit_len) + "b"), read_list()))
    UI = tuple(tuple(int(u[i]) for u in U) for i in range(max_bit_len))
    VI = tuple(tuple(int(v[i]) for v in V) for i in range(max_bit_len))

    A = [[0 for _ in range(N)] for _ in range(N)]
    for k in range(max_bit_len):
        SS = tuple((s, u) for s, u in zip(S, UI[k]))
        TT = tuple((t, v) for t, v in zip(T, VI[k]))
        F = f(SS, TT)
        if F == None:
            print(-1)
            return

        for i in range(N):
            for j in range(N):
                A[i][j] += (F[i][j] << (max_bit_len - 1 - k))
    
    for a in A:
        print(" ".join(map(str, a)))


if __name__ == "__main__":
    main()

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()))
A = [[0]*N for i in range(N)]
for i in range(N):
    for j in range(N):
        for k in range(64):
            u = (U[i]>>k)&1
            v = (V[j]>>k)&1
            if S[i] == 0 and T[j] == 0:
                if u == 1 or v == 1:
                    A[i][j] |= (1<<k)
            elif S[i] == 0 and T[j] == 1:
                if u == 1 and v == 0:
                    print(-1)
                    exit()
                elif u == v == 1:
                    A[i][j] |= 1<<k
            elif S[i] == 1 and T[j] == 0:
                if u == 0 and v == 1:
                    print(-1)
                    exit()
                elif u == v == 1:
                    A[i][j] |= 1<<k
            else:
                if u == 1 and v == 1:
                    A[i][j] |= (1<<k)
for i in range(N):
    if S[i] == 1:
        X = 0
        for j in range(N):
            X |= A[i][j]
        if X != U[i]:
            for j in range(N):
                if T[j] == 0:
                    A[i][j] |= U[i]&(~X)
                    break
for j in range(N):
    if T[j] == 1:
        X = 0
        for i in range(N):
            X |= A[i][j]
        if X != V[j]:
            for i in range(N):
                if S[i] == 0:
                    A[i][j] |= V[j]&(~X)
                    break
for a in A:
    print(*a)

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=[n*[0]for _ in range(n)]
ss=[u[i]if s[i]==0 else 0 for i in range(n)]
tt=[v[i]if t[i]==0 else 0 for i in range(n)]
son=[[]for _ in range(n)]
ton=[[]for _ in range(n)]
sno=[[n-1]*64 for _ in range(n)]
tno=[[n-1]*64 for _ in range(n)]
for i in range(n):
  for j in range(n):
    ans[i][j]=u[i]&v[j]
    if s[i]==0:
      ans[i][j]|=u[i]
      ss[i]&=ans[i][j]
    else:
      ss[i]|=ans[i][j]
    if t[j]==0:
      ans[i][j]|=v[j]
      tt[j]&=ans[i][j]
    else:
      tt[j]|=ans[i][j]
    if s[i]==0 or t[i]==0:
      for k in range(64):
        if ans[i][j]&1<<k:
          if s[i]==0:sno[i][k]-=1
          if t[j]==0:tno[j][k]-=1
    if j==n-1:
      if s[i]==1:
        if ss[i]|u[i]>u[i]:print(-1);exit()
        if ss[i]!=u[i]:
          for k in range(64):
            if u[i]&1<<k:
              if (ss[i]&1<<k)==0:
                son[i].append(k)
          ss[i]=1
        else:ss[i]=0
      else:
        if ss[i]!=u[i]:print(-1);exit()
        else:ss[i]=2
    if i==n-1:
      if t[j]==1:
        if tt[j]|v[j]>v[j]:print(-1);exit()
        if tt[j]!=v[j]:
          for k in range(64):
            if v[j]&1<<k:
              if (tt[j]&1<<k)==0:
                ton[j].append(k)
          tt[j]=1
        else:tt[j]=0
      else:
        if tt[j]!=v[j]:print(-1);exit()
        else:tt[j]=2
for i in range(n):
  for k in son[i]:
    for j in range(n):
      if tt[j]==2:
        if tno[j][k]:
          ans[i][j]|=1<<k
          tno[j][k]-=1
          break
      if j==n-1:print(-1);exit()
for j in range(n):
  for k in ton[j]:
    for i in range(n):
      if ss[i]==2:
        if sno[i][k]:
          ans[i][j]|=1<<k
          sno[i][k]-=1
          break
      if i==n-1:print(-1);exit()
for i in ans:print(*i)

Submission status: Wrong Answer

Solution code:
N = int(input())
Ss = list(map(int, input().split()))
Ts = list(map(int, input().split()))
Us = list(map(int, input().split()))
Vs = list(map(int, input().split()))

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

for i, (s, u) in enumerate(zip(Ss, Us)):
    if s == 0:
        for j in range(N):
            ans[i][j] |= u
    else:
        for j in range(N):
            if Ts[j] == 1:
                ans[i][j] |= u & Vs[j]
            else:
                pass
                #ans[i][j] |= u

for i, (t, v) in enumerate(zip(Ts, Vs)):
    if t == 0:
        for j in range(N):
            ans[j][i] |= v
    else:
        for j in range(N):
            if Ss[j] == 1:
                ans[j][i] |= v & Us[j]
            else:
                pass
                #ans[j][i] |= v

for i, (s, u) in enumerate(zip(Ss, Us)):
    if s == 0:
        chk = ans[i][0]
        for j in range(1, N):
            chk &= ans[i][j]
        if chk != u:
            print(-1)
            exit()
    else:
        chk = 0
        for j in range(N):
            chk |= ans[i][j]
        if chk != u:
            print(-1)
            exit()

for i, (t, v) in enumerate(zip(Ts, Vs)):
    if t == 0:
        chk = ans[0][i]
        for j in range(1, N):
            chk &= ans[j][i]
        if chk != v:
            print(-1)
            exit()
    else:
        chk = 0
        for j in range(N):
            chk |= ans[j][i]
        if chk != v:
            print(-1)
            exit()
for line in ans:
    print(' '.join(map(str, line)))

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()))
matrix=[[0 for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        if s[i]==0 and t[j]==0:
            matrix[i][j]=u[i]|v[j]
        elif s[i]==0 and t[j]==1:
            if u[i]|v[j]!=v[j]:print(-1);exit()
            matrix[i][j]=u[i]
        elif s[i]==1 and t[j]==0:
            if u[i]|t[j]!=u[i]:print(-1);exit()
            matrix[i][j]=v[j]
        else:
            matrix[i][j]=u[i] & v[j]
for i in matrix:print(*i)

Submission status: Wrong Answer

Solution code:
import heapq

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()))
total = [[0]*n for i in range(n)]

works = True
for bit in range(64):
    if not works:
        break
    
    out = [[-1]*n for i in range(n)]
    qInit = []

    done = [False] * (4 * n)
    
    for i in range(n):
        if s[i] != (u[i] & 1):
            for j in range(n):
                if out[i][j] == s[i]:
                    works = False
                    break
                out[i][j] = u[i] & 1
                done[4 * i + 0 + (u[i] & 1)] = True
                done[4 * j + 2 + (u[i] & 1)] = True
        else:
            qInit.append((i,0,s[i]))

    for j in range(n):
        if t[j] != (v[j] & 1):  
            for i in range(n):
                if out[i][j] == t[j]:
                    works = False
                    break
                out[i][j] = v[j] & 1
                done[4 * i + 0 + (v[j] & 1)] = True
                done[4 * j + 2 + (v[j] & 1)] = True
        else:
            qInit.append((j,1,t[j]))

    counts = dict()

    q = []
    for i, d, b in qInit:
        if not done[4 * i + 2 * d + b]:
            if d == 0:
                for j in range(n):
                    count = 0
                    if out[i][j] == -1:
                        count += 1
                heapq.heappush(q, (count,i,d,b))
                counts[(i,d,b)] = count
            else:
                for j in range(n):
                    count = 0
                    if out[j][i] == -1:
                        count += 1
                heapq.heappush(q, (count,i,d,b))
                counts[(i,d,b)] = count
                    
                
    while q:
        count, i, d, b = heapq.heappop(q)
        if not done[4 * i + 2 * d + b]:
            if count == 0:
                works = False
                break
            if d == 0:
                for j in range(n):
                    if out[i][j] == -1:
                        break
                    
                assert(out[i][j] == -1)
                out[i][j] = b
                #done.add((i,0,b))
                done[4 * i + 2 * 0 + b] = True
                #done.add((j,1,b))
                done[4 * j + 2 * 1 + b] = True
                if (i,0,1-b) in counts:
                    counts[(i,0,1-b)] -= 1
                    heapq.heappush(q, (counts[(i,0,1-b)], i, 0, 1 - b))
                if (j,1,1-b) in counts:
                    counts[(j,1,1-b)] -= 1
                    heapq.heappush(q, (counts[(j,1,1-b)], j, 1, 1 - b))
            else:
                j = i
                for i in range(n):
                    if out[i][j] == -1:
                        break
                assert(out[i][j] == -1)
                
                out[i][j] = b
                #done.add((i,0,b))
                done[4 * i + 2 * 0 + b] = True
                #done.add((j,1,b))
                done[4 * j + 2 * 1 + b] = True
                if (i,0,1-b) in counts:
                    counts[(i,0,1-b)] -= 1
                    heapq.heappush(q, (counts[(i,0,1-b)], i, 0, 1 - b))
                if (j,1,1-b) in counts:
                    counts[(j,1,1-b)] -= 1
                    heapq.heappush(q, (counts[(j,1,1-b)], j, 1, 1 - b))
                    
            
        

    #Clean-Up
    mask = 1 << bit
    for i in range(n):
        for j in range(n):
            if out[i][j]:
                total[i][j] |= mask

    for i in range(n):
        u[i] >>= 1
        v[i] >>= 1
    
if works:
    totalS = ['']*n
    for i in range(n):
        totalS[i] = ' '.join(map(str,total[i]))
    print(*totalS,sep='\n')
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()))

matrix = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
  for j in range(n):
    matrix[i][j] = u[i]

flag = True
for j in range(n):
  for i in range(n):
    if t[j] == 0:
      matrix[i][j] = matrix[i][j] | v[j]
      if s[i] == 1:
        matrix[i][j] = matrix[i][j] & v[j]
      if s[i] == 1 and matrix[i][j] & u[i] != matrix[i][j]:
        flag = False
    else:
      matrix[i][j] = matrix[i][j] & v[j]
      if s[i] == 0 and matrix[i][j] | u[i] != matrix[i][j]:
        flag = False

if flag:
  for i in range(n):
    print(" ".join(map(str,matrix[i])))
else:
  print("-1")

Submission status: Wrong Answer

Solution code:
from functools import reduce
import operator

N = int(input())
Ss = list(map(int, input().split()))
Ts = list(map(int, input().split()))
Us = list(map(int, input().split()))
Vs = list(map(int, input().split()))
mat = [[0] * N for _ in range(N)]


class NotOK(Exception):
    pass


try:
    for i in range(N):
        for j in range(N):
            x = 0
            if not Ss[i]:
                x |= Us[i]
            if not Ts[j]:
                x |= Vs[j]
            if Ss[i] and (~Us[i] & x):
                raise NotOK
            if Ts[j] and (~Vs[j] & x):
                raise NotOK
            a = Us[i] & ~x
            b = Vs[j] & ~x
            x |= a & b
            mat[i][j] = x
    for i in range(N):
        vals = mat[i]
        if Ss[i]:
            x = reduce(operator.or_, vals)
        else:
            x = reduce(operator.and_, vals)
        if x != Us[i]:
            raise NotOK
        vals = []
        for j in range(N):
            vals.append(mat[j][i])
        if Ts[i]:
            x = reduce(operator.or_, vals)
        else:
            x = reduce(operator.and_, vals)
        if x != Vs[i]:
            raise NotOK
    for i in range(N):
        print(*mat[i])
except NotOK:
    print(-1)

Submission status: Wrong Answer

Solution code:

"""

おもしろそうｙ！
桁ごとに独立に考えてよい

積で桁が1→ ALL 1 W
積で桁が0→ 少なくとも一つ0 X
和で桁が1→ 少なくとも1つ 1 Y
和で桁が0→ ALL 0 Z

積1,和で0は簡単に埋まる
→ここでぶつかったら当然1

それ以外は列と行が抜けた感じになるので座圧できる
01を1こずつ入れてあげれば安泰
2*2以上なら
01
10…にすればおわり

そうでない=1行しかないなら
1じゃない方を構築して、もう片方を調べればよい

"""
import sys

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 i in range(N) ]

for dig in range(64):

    nd = 2**dig
    XlisR = []
    YlisR = []
    R = []

    Wexist = False
    Zexist = False

    for i in range(N):

        if S[i] == 0:
            if U[i] & nd == 1:
                Wexist = True
                for j in range(N):
                    ans[i][j] |= nd
            else:
                XlisR.append(i)
                R.append(i)

        else:
            if U[i] & nd == 0:
                Zexist = True
            else:
                YlisR.append(i)
                R.append(i)

    WexC = False
    ZexC = False
    XlisC = []
    YlisC = []
    C = []

    for j in range(N):

        if T[j] == 0:
            if V[j] & nd == 1:
                WexC = True
                if Zexist:
                    print (-1)
                    sys.exit()
                for i in range(N):
                    ans[i][j] |= nd
            else:
                XlisC.append(j)
                C.append(j)

        else:
            if V[j] & nd == 0:
                ZexC = True
                if Wexist:
                    print (-1)
                    sys.exit()
            else:
                YlisC.append(j)
                C.append(j)

    if len(C) == 0:
        if len(XlisR) > 0 and not ZexC:
            print (-1)
            sys.exit()
        if len(YlisR) > 0 and not WexC:
            print (-1)
            sys.exit()
    elif len(R) == 0:
        if len(XlisC) > 0 and not Zexist:
            print (-1)
            sys.exit()
        if len(YlisC) > 0 and not Wexist:
            print (-1)
            sys.exit()     

    elif len(C) == 1:
        for i in YlisR:
            for j in C:
                ans[i][j] |= nd

        if len(XlisC) == 1:
            zex = False
            for i in range(N):
                for j in C:
                    if nd & ans[i][j] == 0:
                        zex = True
            if not zex:
                print (-1)
                sys.exit()
        else:
            oex = False
            for i in range(N):
                for j in C:
                    if nd & ans[i][j] > 0:
                        oex = True
            if not oex:
                print (-1)
                sys.exit()

    elif len(R) == 1:
        for i in R:
            for j in YlisC:
                ans[i][j] |= nd
        if len(XlisR) == 1:
            zex = False
            for i in R:
                for j in range(N):
                    if nd & ans[i][j] == 0:
                        zex = True
            if not zex:
                print (-1)
                sys.exit()
        else:
            oex = False
            for i in R:
                for j in range(N):
                    if nd & ans[i][j] > 0:
                        oex = True
            if not oex:
                print (-1)
                sys.exit()

    else:

        for i in range(len(R)):
            for j in range(len(C)):

                ans[R[i]][C[j]] |= nd * ((i+j)%2)

for i in range(N):
    print (*ans[i])