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

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:
#!/usr/bin/env python3
import sys

# case 1には和と積を貪欲にとった場合OK

def solve(N: int, S: "List[int]", T: "List[int]", U: "List[int]", V: "List[int]"):
    result = [[-1 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:
                result[i][j] = U[i] | V[j]
                continue
            if S[i] == 1 and T[j] == 1:
                result[i][j] = U[i] & V[j]
                continue
            if S[i] == 0:
                result[i][j] = U[i]
                # print(i,j)
            if T[j] == 0:
                result[i][j] = V[j]
                # print(i, j)

    for i in range(N):
        isBegin = True
        r = ""
        for j in range(N):
            if not isBegin:
                r += " "
            r+=str(result[i][j])
            isBegin = False
        print(r)

    check = True
    if not check:
        exit()
    for i in range(N):
        if S[i] == 0:
            c = 2**64-1
            for j in range(N):
                c &= result[i][j]
            if c != U[i]:
                print(f"Row constraint {i} not match: actual-{c} ideal-{U[i]}")
        else:
            c = 0
            for j in range(N):
                c |= result[i][j]
            if c != U[i]:
                print(f"Row constraint {i} not match: actual-{c} ideal-{U[i]}")
        if T[i] == 0:
            c = 2 ** 64 - 1
            for j in range(N):
                c &= result[j][i]
            if c != V[i]:
                print(f"Column constraint {i} not match: actual-{c} ideal-{V[i]}")
        else:
            c = 0
            for j in range(N):
                c |= result[j][i]
            if c != V[i]:
                print(f"Column constraint {i} not match: actual-{c} ideal-{V[i]}")
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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: Wrong Answer

Solution code:
def culc(t,v,flg,val):
    if t == 1 and flg ==1:
        return val & v
    elif t == 0 and flg ==0:
        return val | v
    elif t ==1 and flg ==0:
        return marge(val,v)
    else:
        return marge(v,val)

def marge(andv,orv):

    tmp = andv ^ orv
    #print(bin(andv),bin(orv),bin(tmp))
    atp = tmp & andv
    if atp!=0:
        return -1
    return andv







def resolve():
    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()))
    
    table = [[ (s,u) for i in range(N)] for s,u in zip(S,U)]        

    ans = [[0 for i in range(N)] for i in range(N)]
    for col,TV in enumerate(zip(T,V)):
        t,v = TV
        for row in range(N):
            flg,val = table[row][col]
            tmp = culc(t,v,flg,val)
            if tmp == -1:
                print(-1)
                return
            ans[row][col]=tmp
    
    for i in ans:
        print(" ".join(map(str,i)))


        








if __name__ == "__main__":
    #unittest.main()
    resolve()

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:continue
      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:continue
      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:
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

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

    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)]
    ans = [[[-1] * N for _ in range(N)] for _ in range(64)]
    three = []
    two = []
    x = y = 0
    for k in range(64):
        for i in range(N):
            s = S[i]
            u = U[i] >> k & 1
            if s == 0 and u == 1:
                for j in range(N):
                    ans[k][i][j] = 1
            elif s == 0 and u == 0:
                for j in range(N):
                    ans[k][i][j] = 2
                    two.append(i)
            elif s == 1 and u == 0:
                for j in range(N):
                    ans[k][i][j] = 0
            else:
                for j in range(N):
                    ans[k][i][j] = 3
                    three.append(i)
        for j in range(N):
            t = T[j]
            v = V[j] >> k & 1
            if t == 0 and v == 1:
                for i in range(N):
                    if ans[k][i][j] == 0:
                        fin()
                    elif ans[k][i][j] == 2:
                        ans[k][i][j] = 1
            elif t == 1 and v == 0:
                for i in range(N):
                    if ans[k][i][j] == 1:
                        fin()
                    elif ans[k][i][j] == 3:
                        ans[k][i][j] = 0
            elif t == 0 and v == 0:
                ok = 0
                for i in range(N):
                    if not ans[k][i][j] & 1:
                        ok = 1
                        break
                if not ok:
                    if not three:
                        fin()
                    ans[k][three[x]][j] = 0
                    x = (x + 1) % len(three)
            else:
                ok = 0
                for i in range(N):
                    if ans[k][i][j] & 1:
                        ok = 1
                        break
                if not ok:
                    if not two:
                        fin()
                    ans[k][two[y]][j] = 1
                    y = (y+1) % len(two)
        for i in range(N):
            s = S[i]
            u = U[i] >> k & 1
            if s == 0 and u == 0:
                ok = 0
                for j in range(N):
                    if not ans[k][i][j] & 1:
                        ok = 1
                        break
                if not ok:
                    fin()
            elif s == 1 and u == 1:
                ok = 0
                for j in range(N):
                    if ans[k][i][j] & 1:
                        ok = 1
                        break
                if not ok:
                    fin()
        for i in range(N):
            for j in range(N):
                ANS[i][j] += (ans[k][i][j] & 1) << k
    for i in range(N):
        print(*ANS[i])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def culc(t,v,flg,val):
    if t == 1 and flg ==1:
        return val & v
    elif t == 0 and flg ==0:
        return val | v
    elif t ==1 and flg ==0:
        return marge(val,v)
    else:
        return marge(v,val)

def marge(andv,orv):

    tmp = andv ^ orv
    #print(bin(andv),bin(orv),bin(tmp))
    atp = tmp & andv
    if atp!=0:
        return -1
    return orv







def resolve():
    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()))
    
    table = [[ (s,u) for i in range(N)] for s,u in zip(S,U)]        

    ans = [[0 for i in range(N)] for i in range(N)]
    for col,TV in enumerate(zip(T,V)):
        t,v = TV
        for row in range(N):
            flg,val = table[row][col]
            tmp = culc(t,v,flg,val)
            if tmp == -1:
                print(-1)
                return
            ans[row][col]=tmp
    
    for i in ans:
        print(" ".join(map(str,i)))


        








if __name__ == "__main__":
    #unittest.main()
    resolve()

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

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

Submission status: Wrong Answer

Solution code:
import sys
range = xrange
input = raw_input

big = int(2**63 - 1)
big2 = 2**64 - 1
def flipper(x):
    if x > big:
        return int(x - 2**64)
    else:
        return x

n = int(input())
S = [int(x) for x in input().split()]
T = [int(x) for x in input().split()]

Scount = [[0] for _ in range(2)]
for s in S:
    Scount[0].append((Scount[0][-1] + (s == 0)) & 1)
    Scount[1].append((Scount[1][-1] + (s == 1)) & 1)

Tcount = [[0] for _ in range(2)]
for t in T:
    Tcount[0].append((Tcount[0][-1] + (t == 0)) & 1)
    Tcount[1].append((Tcount[1][-1] + (t == 1)) & 1)

U = [flipper(int(x)) for x in input().split()]
V = [flipper(int(x)) for x in input().split()]

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

mat = [[0] * n for _ in range(n)]
for layer in range(64):
    ubit = [1 & (u >> layer) for u in U]
    vbit = [1 & (v >> layer) for v in V]
    for i in range(n):
        for j in range(n):
            if ubit[i] != S[i]:
                mat[i][j] = ubit[i]
            elif vbit[j] != T[j]:
                mat[i][j] = vbit[j]
            elif S[i] == T[j]:
                mat[i][j] = ubit[i]
            else:
                #assert(S[i] != T[j])
                mat[i][j] = Scount[S[i]][i] ^ Tcount[T[j]][j]
    for i in range(n):
        for j in range(n):
            A[i][j] += mat[i][j] << layer

U = [big2 & u for u in U]
V = [big2 & v for v in V]

for i in range(n):
    x = A[i][0]
    if S[i] == 0:
        for j in range(n):
            x &= A[i][j]
    else:
        for j in range(n):
            x |= A[i][j]
    if x != U[i]:
        print -1
        sys.exit()

for j in range(n):
    x = A[0][j]
    if T[j] == 0:
        for i in range(n):
            x &= A[i][j]
    else:
        for i in range(n):
            x |= A[i][j]
    if x != V[j]:
        print -1
        sys.exit()

print '\n'.join(' '.join(str(x) for x in row) for row in A)

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):
    out = [[-1]*n for i in range(n)]
    qInit = []
    
    done = set()
    
    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.add((i,0,u[i] & 1))
                done.add((j,1,u[i] & 1))
        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.add((i,0,v[j] & 1))
                done.add((j,1,v[j] & 1))
        else:
            qInit.append((j,1,t[j]))

    counts = dict()

    q = []
    for i, d, b in qInit:
        if (i,d,b) not in done:
            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 (i,d,b) not in done:
            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.add((j,1,b))
                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.add((j,1,b))
                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):
        if s[i]==0 and t[j]==0:
            matrix[i][j]=u[i]|t[j]
        elif s[i]==0 and t[j]==1:
            if u[i]|t[j]!=t[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:
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:
                if rand <= cor:
                    X[r][c] = CX[c] if RN[r] == CN[c] or CN[c] >= rand else RX[r]
                else:
                    X[r][c] = RX[r] if RN[r] == CN[c] or RN[r] >= cor else CX[c]
        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:
                if rand <= cor:
                    X[r][c] = CX[c] if RN[r] == CN[c] or CN[c] >= rand else RX[r]
                else:
                    X[r][c] = RX[r] if RN[r] == CN[c] or RN[r] >= cor else CX[c]
        else:
            print(1 // 0)

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:
def culc(t,v,flg,val):
    if t == 1 and flg ==1:
        return val & v
    elif t == 0 and flg ==0:
        return val | v
    elif t ==1 and flg ==0:
        return marge(val,v)
    else:
        return marge(v,val)

def marge(andv,orv):
    tmp = andv ^ orv

    atp = tmp & andv
    if atp!=0:
        return -1

    otp = tmp & orv
    return andv + otp







def resolve():
    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()))
    
    table = [[ (s,u) for i in range(N)] for s,u in zip(S,U)]        

    ans = [[0 for i in range(N)] for i in range(N)]
    for col,TV in enumerate(zip(T,V)):
        t,v = TV
        for row in range(N):
            flg,val = table[row][col]
            tmp = culc(t,v,flg,val)
            if tmp == -1:
                print(-1)
                return
            ans[row][col]=tmp
    
    for i in ans:
        print(" ".join(map(str,i)))


        








if __name__ == "__main__":
    resolve()

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
sss = list(map(int, input().split()))
ttt = list(map(int, input().split()))
uuu = list(map(int, input().split()))
vvv = list(map(int, input().split()))

max_d = max(max(uuu).bit_length(), max(vvv).bit_length())
ans = np.zeros((n, n), dtype=np.uint64)

for d in range(max_d):
    k = 1 << d
    ambiguous_i = []
    ambiguous_j = []
    for i, (s, u) in enumerate(zip(sss, uuu)):
        if s == 0:
            if u & k:
                ans[i, :] += k
            else:
                ambiguous_i.append(i)
        else:
            if u & k:
                ambiguous_i.append(i)
    for j, (t, v) in enumerate(zip(ttt, vvv)):
        if t == 0:
            if v & k:
                ans[:, j] += k
            else:
                ambiguous_j.append(j)
        else:
            if v & k:
                ambiguous_j.append(j)

    for ii, i in enumerate(ambiguous_i):
        for jj, j in enumerate(ambiguous_j):
            if (ii ^ jj) & 1:
                ans[i][j] += k

for row in ans:
    print(*row)

Submission status: Wrong Answer

Solution code:
def culc(t,v,flg,val):
    if t == 1 and flg ==1:
        return val & v
    elif t == 0 and flg ==0:
        return val | v
    elif t ==1 and flg ==0:
        return marge(val,v)
    else:
        return marge(v,val)

def marge(andv,orv):
    ans = 0
    tmp = 1
    while andv >0 or orv > 0:
        ab = andv % 2
        ob = orv %2
        if ab == 1 and ob == 0:
            return -1
        elif ab == 0 and ob ==1:
            ans += 0
        elif ab == 1 and ob ==1:
            ans += tmp
        tmp = tmp << 1
        andv = andv >> 1
        orv = orv >> 1
    
    return ans








def resolve():
    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()))
    
    table = [[ (s,u) for i in range(N)] for s,u in zip(S,U)]        

    ans = [[0 for i in range(N)] for i in range(N)]
    for col,TV in enumerate(zip(T,V)):
        t,v = TV
        for row in range(N):
            flg,val = table[row][col]
            tmp = culc(t,v,flg,val)
            if tmp == -1:
                print(-1)
                return
            ans[row][col]=tmp
    
    for i in ans:
        print(" ".join(map(str,i)))


        








if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(map(int,input().split())) 
t = list(map(int,input().split()))
u_all = list(map(int,input().split()))
v_all = list(map(int,input().split()))

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

def solve(k):
    u = [(x >> k) % 2 for x in u_all]
    v = [(x >> k) % 2 for x in v_all]
    ans = [[-1] * n for _ in range(n)]

    free_r = n
    free_c = n

    for i in range(n):
        if s[i] != u[i]:
            free_r -= 1
            for j in range(n):
                ans[i][j] = u[i]

    for i in range(n):
        if t[i] != v[i]:
            free_c -= 1
            for j in range(n):
                if ans[j][i] != -1 and ans[j][i] != v[i]:ng()
                ans[j][i] = v[i]

    if free_r == 1:
        for i in range(n):
            if t[i] == v[i]:
                for j in range(n):
                    if ans[j][i] == -1:
                        ans[j][i] = v[i]

    elif free_c == 1:
        for i in range(n):
            if s[i] == u[i]:
                for j in range(n):
                    if ans[i][j] == -1:
                        ans[i][j] = u[i]

    else:
        start = 0
        for i in range(n):
            num = start
            for j in range(n):
                if ans[i][j] != -1:continue
                ans[i][j] = num
                num ^= 1
            start ^= 1
    return ans
    
    
ans = [[0] * n for _ in range(n)]

for k in range(64):
    tmp_ans = solve(k)
    for i in range(n):
        for j in range(n):
            ans[i][j] += tmp_ans[i][j] << k



for i in range(n):
    if s[i] == 0:
        tmp = ans[i][0]
        for j in range(n):
            tmp &= ans[i][j]
        if tmp != u_all[i]:ng()
    else:
        tmp = ans[i][0]
        for j in range(n):
            tmp |= ans[i][j]
        if tmp != u_all[i]:ng()

for i in range(n):
    if t[i] == 0:
        tmp = ans[0][i]
        for j in range(n):
            tmp &= ans[j][i]
        if tmp != v_all[i]:ng()
    else:
        tmp = ans[0][i]
        for j in range(n):
            tmp |= ans[j][i]
        if tmp != v_all[i]:ng()

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