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,*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 in r:
  for j in r:
   if(u[i]&v[j])|((s[i]^1)*u[i])|((t[j]^1)*v[j]):a[i][j]|=y;l[i]+=1;m[j]+=1
 for i in r:
  if(l[i]==0)*s[i]*u[i]+(m[i]==0)*t[i]*v[i]:
   for j in r:
    if(t[j]^1)*(n-m[j]>1):a[i][j]|=y;m[j]+=1
    if(s[j]^1)*(n-l[j]>1):a[j][i]|=y;l[j]+=1
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):print(-1);exit() 
for i in a:print(*i)

Submission status: Wrong Answer

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 in r:
  for j in r:
   if(u[i]&v[j])|((s[i]^1)*u[i])|((t[j]^1)*v[j]):a[i][j]|=y;l[i]+=1;m[j]+=1
 for i in r:
  if(l[i]==0)*s[i]*u[i]+(m[i]==0)*t[i]*v[i]:
   for j in r:
    if(t[j]^1)*(n-m[j]>1):a[i][j]|=y;m[j]+=1;break
    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):print(-1);exit() 
for i in a:print(*i)

Submission status: Wrong Answer

Solution code:
def main():
  n = int(input())
  S = tuple(map(int, input().split()))
  T = tuple(map(int, input().split()))
  U = tuple(map(lambda x: int(x), input().split()))
  V = tuple(map(lambda x: int(x), input().split()))

  row_or = [-1] * n
  col_or = [-1] * n
  ans = [None] * n

  # Si == 0 の時、i行目は最小でUi
  for i in range(n):
    ans[i] = ([U[i]] if S[i] == 0 else [None]) * n
    row_or[i] = U[i]

  # Tj == 0 の時、j列目は最小でVi
  for j in range(n):
    if T[j] == 1: continue

    v = V[j]
    for i in range(n):
      ans[i][j] = v | ans[i][j] if ans[i][j] != None else v
      col_or[j] = max(col_or[j], ans[i][j])

  for i in range(n):
    for j in range(n):
      if S[i] == 1 and T[j] == 1:
        ans[i][j] = U[i] | V[j]
      elif S[i] == 1 and row_or[i] < U[i]:
        ans[i][j] |= U[i]
        row_or[i] = ans[i][j]
        if row_or[i] > U[i]:
          print(-1)
          exit()
      elif T[j] == 1 and col_or[j] < V[j]:
        ans[i][j] |= V[j]
        col_or[j] = ans[i][j]
        if col_or[j] > V[j]:
          print(-1)
          exit()

  for row in ans:
    print(*row, sep=" ")


main()

Submission status: Wrong Answer

Solution code:
import heapq

def main():
  n = int(input())
  S = tuple(map(int, input().split()))
  T = tuple(map(int, input().split()))
  U = tuple(map(lambda x: int(x), input().split()))
  V = tuple(map(lambda x: int(x), input().split()))

  row_or = [-1] * n
  col_or = [-1] * n
  ans = [None] * n

  # Si == 0 の時、i行目は最小でUi
  for i in range(n):
    ans[i] = ([U[i]] if S[i] == 0 else [None]) * n
    row_or[i] = U[i]

  # Tj == 0 の時、j列目は最小でVi
  for j in range(n):
    if T[i] == 1: continue

    v = V[j]
    for i in range(n):
      ans[i][j] = v & ans[i][j] if ans[i][j] else v
      col_or[j] = max(col_or[j], ans[i][j])

  for i in range(n):
    for j in range(n):
      if S[i] == 1 and T[j] == 1:
        ans[i][j] = U[i] | V[j]
      elif S[i] == 1 and row_or[i] < U[i]:
        ans[i][j] |= U[i]
        row_or[i] = ans[i][j]
        if row_or[i] > U[i]:
          print(-1)
          exit()
      elif T[j] == 1 and col_or[j] < V[j]:
        ans[i][j] |= V[j]
        col_or[j] = ans[i][j]
        if col_or[j] > V[j]:
          print(-1)
          exit()

  for row in ans:
    print(*row, sep=" ")


main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
import math

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

def gen(p, n, s, t, u, v, result):
    a = [ [ -1 for i in range(n) ] for j in range(n) ]
    for i in range(n):
        ub = (u[i] >> p) & 1
        vb = (v[i] >> p) & 1
        if s[i] == 0:
            if ub == 1:
                a[i] = [ 1 for j in range(n) ]
        else:
            if ub == 0:
                a[i] = [ 0 for j in range(n) ]
            
        if t[i] == 0:
            if vb == 1:
                for j in range(n):
                    if a[j][i] == 0:
                        f()
                    a[j][i] = 1
        else:
            if vb == 0:
                for j in range(n):
                    if a[j][i] == 1:
                        f()
                    a[j][i] = 0

    for i in range(n):
        ub = (u[i] >> p) & 1
        if s[i] == 1 and ub == 1:
            if 1 in a[i]:
                continue
            
            for j in range(n):
                vb = (v[j] >> p) & 1
                if t[j] == 1 and vb == 1:
                    a[i][j] = 1
                    break

                if t[j] == 0 and vb == 0:
                    if [ a[k][j] for k in range(n) ].count(1) < n - 1:
                        a[i][j] = 1
                        break;
            else:
                f()
                
    for j in range(n):
        vb = (v[j] >> p) & 1
        if t[j] == 1 and vb == 1:
            if 1 in [ a[k][j] for k in range(n) ]:
                continue

            for i in range(n):
                ub = (u[i] >> p) & 1
                if s[i] == 1 and ub == 1:
                    a[i][j] = 1
                    break

                if s[i] == 0 and ub == 0:
                    if a[i].count(1) < n - 1:
                        a[i][j] = 1
                        break
            else:
                f()

    for i in range(n):
        for j in range(n):
            v = a[i][j] if a[i][j] >= 0 else 0
            result[i][j] = result[i][j] | (v << p)

n = int(input())
s = tuple(map(int, input().split()))
t = tuple(map(int, input().split()))
u = tuple(map(int, input().split()))
v = tuple(map(int, input().split()))

p = math.log2(max(u + v))

if p.is_integer():
    p = int(p) + 1
else:
    p = int(math.ceil(p))
    
result = [ [ 0 for i in range(n) ] for j in range(n) ]


for i in range(p):
    gen(i, n, s, t, u, v, result)

for i in range(n):
    print(' '.join(map(str, result[i])))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
import bisect

n = int(input())
s = tuple(map(int, input().split()))
t = tuple(map(int, input().split()))
u = tuple(map(int, input().split()))
v = tuple(map(int, input().split()))
       
a = [ [ -1 for i in range(n) ] for j in range(n) ]

si = [ False for i in range(n) ]
sj = [ False for j in range(n) ]

#phase 1
for i in range(n):
    if s[i] == 0:
        if u[i] > 0:
            a[i] = [ u[i] for j in range(n) ]
            si[i] = True
    else:
        if u[i] == 0:
            a[i] = [ 0 for j in range(n) ]
            si[i] = True            

    if t[i] == 0:
        if v[i] > 0:
            for j in range(n):
                if a[j][i] == 0:
                    print(-1)
                    exit()
                a[j][i] = v[i]
            sj[i] = True
    else:
        if v[i] == 0:
            for j in range(n):
                if a[j][i] > 0:
                    print(-1)
                    exit()                
                a[j][i] = 0
            sj[i] = True

#phase 2
for i in range(n):
    for j in range(n):
        if a[i][j] < 0:
            if si[i] and sj[j]:
                a[i][j] = 0
            elif si[i]:
                if t[j] == 0:
                    a[i][j] = 0
                else:
                    a[i][j] = v[j]
                sj[j] = True
            elif sj[j]:                
                if s[i] == 0:
                    a[i][j] = 0
                else:
                    a[i][j] = u[i]
                si[i] = True                
            else:
                if s[i] == 0 and t[j] == 0:
                    a[i][j] = 0
                    si[i] = True
                    sj[j] = True
                elif s[i] == 0 and t[j] == 1:
                    a[i][j] = 0
                    si[i] = True                    
                elif s[i] == 1 and t[j] == 0:
                    a[i][j] = u[i]
                    si[i] = True                                        
                else:
                    if u[i] == v[j]:
                        a[i][j] = u[i]
                        si[i] = True
                        sj[j] = True
                    else:
                        a[i][j] = u[i]
                        si[i] = True

if False in si + sj:
    print(-1)
    exit()
    
for i in range(n):
    print(' '.join(map(str, a[i])))

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)]
counts_r = [[0] * 64 for _ in range(N)]
counts_c = [[0] * 64 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
            if Ss[i] and Ts[j]:
                x |= Us[i] & Vs[j]
            mat[i][j] = x
            for k in range(64):
                if (x >> k) & 1:
                    counts_r[i][k] += 1
                    counts_c[j][k] += 1

    def corr(x, vals, ors, counts):
        for k in range(64):
            if (x >> k) & 1 == 0:
                continue
            for j in range(N):
                if ors[j]:
                    continue
                if counts[j][k] >= N - 1:
                    continue
                counts[j][k] += 1
                vals[j] += 1 << k
                x -= 1 << k
                if not x:
                    return x
        return x

    for i in range(N):
        vals = mat[i]
        if Ss[i]:
            x = reduce(operator.or_, vals)
            if corr(Us[i] & ~x, vals, Ts, counts_c):
                raise NotOK
        vals = []
        for j in range(N):
            vals.append(mat[j][i])
        if Ts[i]:
            x = reduce(operator.or_, vals)
            if corr(Vs[i] & ~x, vals, Ss, counts_r):
                raise NotOK
            for j in range(N):
                mat[j][i] = vals[j]
    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:
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)]
counts_r = [[0] * 64 for _ in range(N)]
counts_c = [[0] * 64 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
            if Ss[i] and Ts[j]:
                x |= Us[i] & Vs[j]
            mat[i][j] = x
            for k in range(64):
                if (x >> k) & 1:
                    counts_r[i][k] += 1
                    counts_c[j][k] += 1

    def corr(x, vals, ors, counts):
        for k in range(64):
            if (x >> k) & 1 == 0:
                continue
            for j in range(N):
                if ors[j]:
                    continue
                if counts[j][k] >= N - 1:
                    continue
                counts[j][k] += 1
                vals[i] += 1 << k
                x -= 1 << k
                if not x:
                    return x
        return x

    for i in range(N):
        vals = mat[i]
        if Ss[i]:
            x = reduce(operator.or_, vals)
            if corr(Us[i] & ~x, vals, Ts, counts_c):
                raise NotOK
        vals = []
        for j in range(N):
            vals.append(mat[j][i])
        if Ts[i]:
            x = reduce(operator.or_, vals)
            if corr(Vs[i] & ~x, vals, Ss, counts_r):
                raise NotOK
            for j in range(N):
                mat[j][i] = vals[j]
    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:
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)]
counts_r = [[0] * 64 for _ in range(N)]
counts_c = [[0] * 64 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
            if Ss[i] and Ts[j]:
                x |= Us[i] & Vs[j]
            mat[i][j] = x
            for k in range(64):
                if (x >> k) & 1:
                    counts_r[i][k] += 1
                    counts_c[j][k] += 1

    def corr(x, vals, ors, counts):
        for j in range(N):
            if ors[j]:
                continue
            for k in range(64):
                if (x >> k) & 1 == 0:
                    continue
                if counts[j][k] >= N - 1:
                    continue
                counts[j][k] += 1
                vals[i] += 1 << k
                x -= 1 << k
                if not x:
                    return x
        return x

    for i in range(N):
        vals = mat[i]
        if Ss[i]:
            x = reduce(operator.or_, vals)
            if corr(Us[i] & ~x, vals, Ts, counts_c):
                raise NotOK
        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)
            if corr(Vs[i] & ~x, vals, Ss, counts_r):
                raise NotOK
            for j in range(N):
                mat[j][i] = vals[j]
        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:
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)]
counts_r = [[0] * 64 for _ in range(N)]
counts_c = [[0] * 64 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
            if Ss[i] and Ts[j]:
                x |= Us[i] & Vs[j]
            mat[i][j] = x
            for k in range(64):
                if (x >> k) & 1:
                    counts_r[i][k] += 1
                    counts_c[j][k] += 1

    def corr(x, vals, ors, counts):
        for j in range(N):
            if ors[j]:
                continue
            for k in range(64):
                if (x >> k) & 1 == 0:
                    continue
                if counts_c[j][k] >= N - 1:
                    continue
                counts_c[j][k] += 1
                vals[i] += 1 << k
                x -= 1 << k
                if not x:
                    return x
        return x

    for i in range(N):
        vals = mat[i]
        if Ss[i]:
            x = reduce(operator.or_, vals)
            if corr(Us[i] & ~x, vals, Ts, counts_c):
                raise NotOK
        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)
            if corr(Vs[i] & ~x, vals, Ss, counts_r):
                raise NotOK
            for j in range(N):
                mat[j][i] = vals[j]
        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:
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)]
counts_r = [[0] * 64 for _ in range(N)]
counts_c = [[0] * 64 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
            if Ss[i] and Ts[j]:
                x |= Us[i] & Vs[j]
            mat[i][j] = x
            for k in range(64):
                if (x >> k) & 1:
                    counts_r[i][k] += 1
                    counts_c[j][k] += 1

    def corr(x, vals, ors, counts):
        for j in range(N):
            if ors[j]:
                continue
            for k in range(64):
                if (x >> k) & 1 == 0:
                    continue
                if counts_c[j][k] >= N - 1:
                    continue
                counts_c[j][k] += 1
                vals[i] += 1 << k
                x -= 1 << k
                if not x:
                    return x
        return x

    for i in range(N):
        vals = mat[i]
        if Ss[i]:
            x = reduce(operator.or_, vals)
            if corr(Us[i] & ~x, vals, Ts, counts_c):
                raise NotOK
        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)
            if corr(Us[i] & ~x, vals, Ts, counts_c):
                raise NotOK
            for j in range(N):
                mat[j][i] = vals[j]
        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:
import math
import sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from heapq import heapify, heappop, heappush
from itertools import chain, combinations, permutations, product
from typing import List, Dict

import numpy as np

sys.setrecursionlimit(10**5 + 1)
input = sys.stdin.readline
write = sys.stdout.write


def read_int() -> int:
    return int(input())


def read_n_int() -> list:
    return [int(s) for s in input().split()]


def list2d(row, col, init=0) -> List[list]:
    return [[init] * col for _ in range(row)]


# MOD = 10**9 + 7
MOD = 998244353

N = read_int()
row_type, col_type = [np.array(read_n_int(), 'bool') for _ in range(2)]
u, v = [np.array(read_n_int(), 'u8') for _ in range(2)]


def fill_empty(row_type, col_type, row_val, col_val):
    A = np.full((N, N), 2, dtype='u8')

    all_one_row = (row_type == 0) & (row_val == 1)
    all_zero_row = (row_type == 1) & (row_val == 0)
    all_one_col = (col_type == 0) & (col_val == 1)
    all_zero_col = (col_type == 1) & (col_val == 0)

    A[all_one_row] = 1
    A[all_zero_row] = 0

    A[:, all_one_col] = 1
    A[:, all_zero_col] = 0

    for i, r_val in enumerate(row_val):
        # empty = A[i] == 2
        # same = col_type == row_type[i]
        # or_val = r_val | col_val
        # and_val = r_val & col_val

        # A[i, empty & same] = or_val[empty & same]
        # A[i, empty & ~same] = and_val[empty & ~same]

        for j in range(N):
            if A[i, j] == 2:
                if row_type[i] == col_type[j]:
                    A[i, j] = r_val | col_val[j]
                else:
                    A[i, j] = r_val & col_val[j]
    if check(A, row_type, col_type, row_val, col_val):
        return A
    else:
        return None


def check(A, s, t, u, v):
    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


ans = np.zeros((N, N), dtype='u8')

for i in range(64):
    temp = fill_empty(row_type, col_type, (u >> i) & 1, (v >> i) & 1)
    if temp is not None:
        ans += temp << i
    else:
        print(-1)
        exit(0)

for a in ans:
    print(*a, sep=' ')

Submission status: Wrong Answer

Solution code:
import math
import sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from heapq import heapify, heappop, heappush
from itertools import chain, combinations, permutations, product
from typing import List, Dict

import numpy as np

sys.setrecursionlimit(10**5 + 1)
input = sys.stdin.readline
write = sys.stdout.write


def read_int() -> int:
    return int(input())


def read_n_int() -> list:
    return [int(s) for s in input().split()]


def list2d(row, col, init=0) -> List[list]:
    return [[init] * col for _ in range(row)]


# MOD = 10**9 + 7
MOD = 998244353

N = read_int()
row_type, col_type = [np.array(read_n_int(), 'bool') for _ in range(2)]
u, v = [np.array(read_n_int(), 'u8') for _ in range(2)]


def fill_empty(row_type, col_type, row_val, col_val):
    A = np.full((N, N), 2, dtype='u8')

    all_one_row = (row_type == 0) & (row_val == 1)
    all_zero_row = (row_type == 1) & (row_val == 0)
    all_one_col = (col_type == 0) & (col_val == 1)
    all_zero_col = (col_type == 1) & (col_val == 0)

    A[all_one_row] = 1
    A[all_zero_row] = 0

    A[:, all_one_col] = 1
    A[:, all_zero_col] = 0

    for i, r_val in enumerate(row_val):
        empty = A[i] == 2
        same = col_type == row_type[i]
        or_val = r_val | col_val
        and_val = r_val & col_val

        A[i, empty & same] = or_val[empty & same]
        A[i, empty & ~same] = and_val[empty & ~same]

        # for j in range(N):
        #     if A[i, j] == 2:
        #         if row_type[i] == col_type[j]:
        #             A[i, j] = r_val | col_val[j]
        #         else:
        #             A[i, j] = r_val & col_val[j]
    if check(A, row_type, col_type, row_val, col_val):
        return A
    else:
        return None


def check(A, s, t, u, v):
    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


ans = np.zeros((N, N), dtype='u8')

for i in range(64):
    temp = fill_empty(row_type, col_type, (u >> i) & 1, (v >> i) & 1)
    if temp is not None:
        ans += temp << i
    else:
        print(-1)
        exit(0)

for a in ans:
    print(*a, sep=' ')

Submission status: Wrong Answer

Solution code:
import math
import sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from heapq import heapify, heappop, heappush
from itertools import chain, combinations, permutations, product
from typing import List, Dict

import numpy as np

sys.setrecursionlimit(10**5 + 1)
input = sys.stdin.readline
write = sys.stdout.write


def read_int() -> int:
    return int(input())


def read_n_int() -> list:
    return [int(s) for s in input().split()]


def list2d(row, col, init=0) -> List[list]:
    return [[init] * col for _ in range(row)]


# MOD = 10**9 + 7
MOD = 998244353

N = read_int()
row_type, col_type, u, v = [np.array(read_n_int(), 'u8') for _ in range(4)]


def fill_empty(row_type, col_type, row_val, col_val):
    A = np.zeros((N, N), dtype='u8') + 2

    all_one_row = (row_type == 0) & (row_val == 1)
    all_zero_row = (row_type == 1) & (row_val == 0)
    all_one_col = (col_type == 0) & (col_val == 1)
    all_zero_col = (col_type == 1) & (col_val == 0)

    A[all_one_row] = 1
    A[all_zero_row] = 0

    A[:, all_one_col] = 1
    A[:, all_zero_col] = 0

    for i in range(N):
        empty = A[i] == 2
        same = col_type == row_type[i]
        or_val = row_val[i] | col_val
        and_val = row_val[i] & col_val

        A[i, empty & same] = or_val[empty & same]
        A[i, empty & ~same] = and_val[empty & ~same]
    return A


def check(A, s, t, u, v):
    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


ans = np.zeros((N, N), dtype='u8')

for i in range(64):
    ans += fill_empty(row_type, col_type, (u >> i) & 1, (v >> i) & 1) << i
# print(ans)
if check(ans, row_type, col_type, u, v):
    for a in ans:
        print(*a, sep=' ')
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import math
import sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from heapq import heapify, heappop, heappush
from itertools import chain, combinations, permutations, product
from typing import List, Dict
from operator import and_, or_

import numpy as np

sys.setrecursionlimit(10**5 + 1)
input = sys.stdin.readline
write = sys.stdout.write


def read_int() -> int:
    return int(input())


def read_n_int() -> list:
    return [int(s) for s in input().split()]


def list2d(row, col, init=0) -> List[list]:
    return [[init] * col for _ in range(row)]


# MOD = 10**9 + 7
MOD = 998244353


def fill_empty(a, row_type, col_type, row_val, col_val):
    # print(col_type, col_val)
    for i in range(N):
        if row_type[i] ^ row_val[i]:
            a[i] = row_val[i]
        if col_type[i] ^ col_val[i]:
            a[:, i] = col_val[i]

    # print(a)
    for i in range(N):
        for j in range(N):
            if a[i, j] == 2:
                if row_type[i] == col_type[j]:
                    a[i, j] = row_val[i] | col_val[j]
                else:
                    a[i, j] = row_val[i] & col_val[j]


def check(a, row_type, col_type, r, c):
    ops = {0: and_, 1: or_}
    init = {0: np.uint64(2**64-1), 1: np.uint64(0)}

    for i in range(N):
        rt, ct = row_type[i], col_type[i]
        if reduce(ops[rt], a[i], init[rt]) != r[i]:
            # print('row', i)
            return False
        if reduce(ops[ct], a[:, i], init[ct]) != c[i]:
            # print('col', i)
            return False
    return True



if __name__ == "__main__":
    N = read_int()
    row_type, col_type, u, v = [np.array(read_n_int(), 'u8') for _ in range(4)]
    ans = np.zeros((N, N), dtype='u8')

    for i in range(64):
        a = np.zeros((N, N), dtype='u8') + 2
        # print('origin:\n', a)
        row_val = (u >> i) & 1
        col_val = (v >> i) & 1
        # print('type:')
        # print(row_type, col_type)
        # print('val')
        # print(row_val, col_val)
        fill_empty(a, row_type, col_type, row_val, col_val)
        ans += a << i
    # print(ans)
    if check(ans, row_type, col_type, u, v):
        for a in ans:
            print(*a, sep=' ')
    else:
        print(-1)