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:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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()))
from collections import defaultdict

def sub(s,t,u,v,n):
    d1 = defaultdict(set)
    d2 = defaultdict(set)
    for i, (ss,uu) in enumerate(zip(s,u)):
        d1[ss,uu].add(i)
    for i, (tt,vv) in enumerate(zip(t,v)):
        d2[tt,vv].add(i)

    if (d1[0,1] and d2[1,0]) or (d1[1,0] and d2[0,1]):
        ans = -1
    else:
        ans = [[0]*n for _ in range(n)]
        dr = []
        dc = []
        for i in d1[0,1]:
            dr.append(1)
            for j in range(n):
                ans[i][j] = 1
        for i in range(n):
            dc.append(1)
            for j in d2[0,1]:
                ans[i][j] = 1
        for i in d1[1,0]:
            dr.append(0)
            for j in range(n):
                ans[i][j] = 0
        for i in range(n):
            dc.append(0)
            for j in d2[1,0]:
                ans[i][j] = 0
        for i in range(n):
            for j in range(n):
                if u[i]==v[j]:
                    ans[i][j] = u[i]

        for i in range(n):
            if s[i] and u[i] and (1 not in ans[i]):
                if d2[0,0]:
                    j = list(d2[0,0])[0]
                    ans[i][j] = 1
                    if 0 not in ans[i]:
                        ans = -1
                        break
                else:
                    ans = -1
                    break
        for j in range(n):
            if t[j] and v[j] and (1 not in [ans[i][j] for i in range(n)]):
                if d1[0,0]:
                    i = list(d1[0,0])[0]
                    ans[i][j] = 1
                    if 0 not in [ans[i][j] for i in range(n)]:
                        ans = -1
                        break
                else:
                    ans = -1
                    break
    return ans
ans = []
val = [[0]*n for _ in range(n)]
for i in range(64):
    ss = [item>>i&1 for item in s]
    tt = [item>>i&1 for item in t]
    uu = [item>>i&1 for item in u]
    vv = [item>>i&1 for item in v]
    res = sub(ss,tt,uu,vv,n)
    if res==-1:
        ans = -1
        break
    else:
        for ii in range(n):
            for jj in range(n):
                val[ii][jj] += res[ii][jj]*(1<<i)
if ans!=-1:
    for i in range(n):
        write(" ".join(map(str, val[i])))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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()))
from collections import defaultdict

def sub(s,t,u,v,n):
    d1 = defaultdict(set)
    d2 = defaultdict(set)
    for i, (ss,uu) in enumerate(zip(s,u)):
        d1[ss,uu].add(i)
    for i, (tt,vv) in enumerate(zip(t,v)):
        d2[tt,vv].add(i)

    if (d1[0,1] and d2[1,0]) or (d1[1,0] and d2[0,1]):
        ans = -1
    else:
        ans = [[0]*n for _ in range(n)]
        dr = []
        dc = []
        for i in d1[0,1]:
            dr.append(1)
            for j in range(n):
                ans[i][j] = 1
        for i in range(n):
            dc.append(1)
            for j in d2[0,1]:
                ans[i][j] = 1
        for i in d1[1,0]:
            dr.append(0)
            for j in range(n):
                ans[i][j] = 0
        for i in range(n):
            dc.append(0)
            for j in d2[1,0]:
                ans[i][j] = 0
        for i in range(n):
            for j in range(n):
                if u[i]==v[j]:
                    ans[i][j] = u[i]

        for i in range(n):
            if s[i] and u[i] and (1 not in ans[i]):
                if d2[0,0]:
                    j = list(d2[0,0])[0]
                    ans[i][j] = 1
                    if 0 not in ans[i]:
                        ans = -1
                        break
                else:
                    ans = -1
                    break
        for j in range(n):
            if t[j] and v[j] and (1 not in [ans[i][j] for i in range(n)]):
                if d1[0,0]:
                    i = list(d1[0,0])[0]
                    ans[i][j] = 1
                    if 0 not in [ans[i][j] for i in range(n)]:
                        ans = -1
                        break
                else:
                    ans = -1
                    break
    return ans
ans = []
for i in range(64):
    ss = [item>>i&1 for item in s]
    tt = [item>>i&1 for item in t]
    uu = [item>>i&1 for item in u]
    vv = [item>>i&1 for item in v]
    res = sub(ss,tt,uu,vv,n)
    if res==-1:
        ans = -1
        break
    else:
        ans.append(res)
if ans!=-1:
    val = [[0]*n for _ in range(n)]
    for ind,item in enumerate(ans):
        for i in range(n):
            for j in range(n):
                val[i][j] += item[i][j]*(1<<ind)
    for i in range(n):
        write(" ".join(map(str, val[i])))

Submission status: Wrong Answer

Solution code:
def main(N,S,T,U,V):
    ans = [[0]*N for _ in range(N)]
    possible = True
    for n in range(64):
        n = 2**n
        n1 = 0
        n2 = 0
        S_ = [False]*N
        T_ = [False]*N
        i1 = -1
        j1 = -1
        i2 = -1
        j2 = -1
        for i in range(N):
            s = S[i]
            u = U[i]&n
            ansi = ans[i]
            if u>0:
                u = 1
            for j in range(N):
                t = T[j]
                v = V[j]&n
                if v>0:
                    v = 1
                if (s,u)==(0,1):
                    if (t,v)!=(1,0):
                        ansi[j] += n
                    else:
                        possible = False
                        break
                elif (s,u)==(1,0):
                    if (t,v)==(0,1):
                        possible = False
                        break
                elif (t,v)==(0,1) or (t,v)==(1,0):
                    ansi[j] += v*n
                elif (s,u)==(t,v):
                    ansi[j] += u*n
                elif (s,u)==(0,0):
                    if n1==0:
                        i1 = i
                        j1 = j
                    else:
                        if i==i1 or j==j1:
                            ansi[j] += n
                    n1 += 1
                elif (s,u)==(1,1):
                    if n2==0:
                        i2 = i
                        j2 = j
                    else:
                         if i==i2 or j==j2:
                             ansi[j] += n
                    n2 += 1
                if u*n==ansi[j]&n:
                    S_[i] = True
                if v*n==ansi[j]&n:
                    T_[j] = True
        if sum(S_)!=N:
            ans[i2][j2] += n
            m = n
            for i in range(N):
                m &= ansi[j2]
            if m&n:
                possible = False
        if sum(T_)!=N:
            ans[i1][j1] += n
            m = n
            for j in range(N):
                m &= ans[i1][j]
            if m&n:
                possible = False
        if not possible:
            break

    if possible:
        for i in range(N):
            print(*ans[i])
    else:
        print(-1)

if __name__ == '__main__':
    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()))
    main(N,S,T,U,V)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
def f(s):
    return list(map(int,s))
def main():
    N=int(sys.stdin.readline())
    S=tuple(map(int,sys.stdin.readline().split()))
    T=tuple(map(int,sys.stdin.readline().split()))
    U=list(map(f,map(lambda x:format(int(x),'064b'),sys.stdin.readline().split())))
    V=list(map(f,map(lambda x:format(int(x),'064b'),sys.stdin.readline().split())))
    A=np.full((N,N,64),-1,dtype=int)
    for i,s,u in zip(range(N),S,U):
        if s==1:
            for j in range(64):
                if u[j]==0:A[i,:,j]=0
        else:
            for j in range(64):
                if u[j]==1:A[i,:,j]=1
    for i,t,v in zip(range(N),T,V):
        if t==1:
            for j in range(64):
                if v[j]==0:A[:,i,j]=0
        else:
            for j in range(64):
                if v[j]==1:A[:,i,j]=1
    for i,s,u in zip(range(N),S,U):
        if s==0:
            for j in range(64):
                if u[j]==0:
                    for k in range(N):
                        if A[i,k,j]==-1:
                            A[i,k,j]=0
                            break
        else:
            for j in range(64):
                if u[j]==1:
                    for k in range(N):
                        if A[i,k,j]==-1:
                            A[i,k,j]=1
                            break
    for i,t,v in zip(range(N),T,V):
        if t==0:
            for j in range(64):
                if v[j]==0:
                    for k in range(N):
                        if A[k,i,j]==-1:
                            A[k,i,j]=0
                            break
        else:
            for j in range(64):
                if v[j]==1:
                    for k in range(N):
                        if A[k,i,j]==-1:
                            A[k,i,j]=1
                            break
    A=np.where(A==-1,0,A)
    B=np.zeros((N,N),dtype=int)
    for r in A:
        for a in r:
            print(int(''.join(list(map(str, a))),2),end=' ')
        print('')
if __name__=='__main__':main()

Submission status: Wrong Answer

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


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

    ans = [[0]*N for _ in range(N)]
    keta = 1
    for n in range(len(bin(max(max(U), max(V))))-2):
        # 下の桁から見ていく
        # 和が0であることが必要なもの、積が1であることが必要なものはいくつありますか
        row_wa_0_count = 0
        row_seki_1_count = 0
        for s, u in zip(S, U):
            u = u >> n
            if s == 1 and u & 1 == 0:
                row_wa_0_count += 1
            elif s == 0 and u & 1 == 1:
                row_seki_1_count += 1
        col_wa_0_count = 0
        col_seki_1_count = 0
        for t, v in zip(T, V):
            v = v >> n
            if t == 1 and v & 1 == 0:
                col_wa_0_count += 1
            elif t == 0 and v & 1 == 1:
                col_seki_1_count += 1

        # ダメなパターンをチェック
        if (row_wa_0_count > 0 and col_seki_1_count > 0) or\
           (row_seki_1_count > 0 and col_wa_0_count > 0):
            print(-1)
            return

        iset = set()
        jset = set()
        # 問題ないなら、構成していく
        # print("--")
        for i in range(N):
            for j in range(N):
                ans[i][j] <<= 1
                u = (U[i] >> n) & 1
                v = (V[j] >> n) & 1
                # print((u, v), end=" ")
                if (S[i], u) == (0, 1) or (T[j], v) == (0, 1):
                    ans[i][j] |= 1
                elif (S[i], u) == (1, 0) or (T[j], v) == (1, 0):
                    ans[i][j] |= 0
                else:
                    # 特に要請がない場合、どのように決めようか。
                    # 全体を見て、斜めに決めたりできたらいい
                    # 0と1のバランスが大事
                    iset.add(i)
                    jset.add(j)
            # print("")
        iset = sorted(iset)
        jset = sorted(jset)
        # 束縛のないところに、単位行列っぽく斜めに1を配置する。
        for i, j in zip(iset, jset):
            ans[i][j] |= 1

        keta <<= 1

    # 手順でbit逆向きになってるので戻して出力な
    for i in range(N):
        c = [0]*N
        for j in range(N):
            b = ans[i][j]
            a = 0
            while b != 0:
                a <<= 1
                a |= b & 1
                b >>= 1
            c[j] = a
        print(*c)
    return


def main():

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
print("1")

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,k in e(u):
  for j,h in e(v):
   if(k&h)|((s[i]^1)*k)|((t[j]^1)*h):a[i][j]|=y;l[i]+=1;m[j]+=1
 for i in r:
  if(l[i]==0)*s[i]*u[i]:
   for j in r:
    if(t[j]^1)*(n-m[j]>1):a[i][j]|=y;m[j]+=1;break
  if(m[i]==0)*t[i]*v[i]:
   for j in r:
    if(s[j]^1)*(n-l[j]>1):a[j][i]|=y;l[j]+=1;break
h=a[0][:];w=[i[0]for i in a]
for i,k in e(a):
 for j,l in e(k):w[i]=(l|w[i]if s[i]else w[i]&l);h[j]=(l|h[j]if t[j]else h[j]&l)
print(-1if(g!=h)|(f!=w)else"\n".join(["".join(map(str,i))for i in a]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
sys.setrecursionlimit(300000)
from pprint import pprint

def _count_bit(v):
    ret = 0
    while v > 0:
        v >>= 1
        ret += 1
    return ret

def solve(N: int, S: "List[int]", T: "List[int]", U: "List[int]", V: "List[int]"):
    bits = 0
    for i in range(N):
        bits = max(bits, _count_bit(U[i]))

    ret = [[[-1] * N for _ in range(N)] for _ in range(bits)]
    for k in range(bits):
        mask = 1 << k
        used = [[0, 0], [0, 0]]
        uns  = [[0, 0], [0, 0]]
        unused = [[], []]
        for i in range(N):
            b = 1 if (U[i] & mask) else 0
            if S[i] == 0 and b == 1:
                used[0][1] = 1
                for j in range(N):
                    ret[k][i][j] = 1
            elif S[i] == 1 and b == 0:
                used[0][0] = 1
                for j in range(N):
                    ret[k][i][j] = 0
            else:
                unused[0].append((i, b))
                uns[0][b] = 1

        for j in range(N):
            b = 1 if (V[j] & mask) else 0
            if T[j] == 0 and b == 1:
                if used[0][0]:
                    print(-1)
                    return -1
                used[1][1] = 1
                for i in range(N):
                    ret[k][i][j] = 1
            elif T[j] == 1 and b == 0:
                if used[0][1]:
                    print(-1)
                    return -1
                used[1][0] = 1
                for i in range(N):
                    ret[k][i][j] = 0
            else:
                unused[1].append((j, b))
                uns[1][b] = 1


        #pprint(ret[k])
        # fill 0
        if used[0][1] and used[1][1]:
            for i in range(N):
                for j in range(N):
                    if ret[k][i][j] < 0:
                        ret[k][i][j] = 0
        elif sum(used[1]) == 0:
            if len(unused[0]) == 0:
                if (sum(used[0]) == 1 and
                    (used[0][0] and uns[1][1] or used[0][1] and uns[1][0])):
                    print(-1)
                    return -1
            elif len(unused[0]) == 1:
                i, b = unused[0][0]
                for j in range(N):
                    ret[k][i][j] = b
                if sum(used[0]) == 1:
                    count = 0
                    for j, jb in unused[1]:
                        if jb != b:
                            ret[k][i][j] = jb
                            count += 1
                    if count == N:
                        print(-1)
                        return -1
            else:
                for j in range(N):
                    idx = j % len(unused[0])
                    i, _ = unused[0][idx]
                    ret[k][i][j] = 0
        elif sum(used[0]) == 0:
            if len(unused[1]) == 0:
                if (sum(used[1]) == 1 and
                    (used[1][0] and uns[0][1] or used[1][1] and uns[0][0])):
                    print(-1)
                    return -1
            elif len(unused[1]) == 1:
                j, b = unused[1][0]
                for i in range(N):
                    ret[k][i][j] = b
                if sum(used[1]) == 1:
                    count = 0
                    for i, ib in unused[0]:
                        if ib != b:
                            ret[k][i][j] = ib
                            count += 1
                    if count == N:
                        print(-1)
                        return -1
            else:
                for i in range(N):
                    idx = i % len(unused[1])
                    j, _ = unused[1][idx]
                    ret[k][i][j] = 0

        #pprint(ret[k])
        #print(' -- ')
        for i in range(N):
            for j in range(N):
                if ret[k][i][j] < 0:
                    ret[k][i][j] = 1


    #pprint(ret)
    for i in range(N):
        row = []
        for j in range(N):
            tmp = 0
            for k in range(bits):
                tmp += (ret[k][i][j] << k)
            row.append(tmp)
        print(' '.join([str(r) for r in row]))

    return

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
sys.setrecursionlimit(300000)
from pprint import pprint

def _count_bit(v):
    ret = 0
    while v > 0:
        v >>= 1
        ret += 1
    return ret

def solve(N: int, S: "List[int]", T: "List[int]", U: "List[int]", V: "List[int]"):
    bits = 0
    for i in range(N):
        bits = max(bits, _count_bit(U[i]))

    ret = [[[-1] * N for _ in range(N)] for _ in range(bits)]
    for k in range(bits):
        mask = 1 << k
        used = [[0, 0], [0, 0]]
        uns  = [[0, 0], [0, 0]]
        unused = [[], []]
        for i in range(N):
            b = 1 if (U[i] & mask) else 0
            if S[i] == 0 and b == 1:
                used[0][1] = 1
                for j in range(N):
                    ret[k][i][j] = 1
            elif S[i] == 1 and b == 0:
                used[0][0] = 1
                for j in range(N):
                    ret[k][i][j] = 0
            else:
                unused[0].append((i, b))
                uns[0][b] = 1

        for j in range(N):
            b = 1 if (V[j] & mask) else 0
            if T[j] == 0 and b == 1:
                if used[0][0]:
                    print(-1)
                    return -1
                used[1][1] = 1
                for i in range(N):
                    ret[k][i][j] = 1
            elif T[j] == 1 and b == 0:
                if used[0][1]:
                    print(-1)
                    return -1
                used[1][0] = 1
                for i in range(N):
                    ret[k][i][j] = 0
            else:
                unused[1].append((j, b))
                uns[1][b] = 1

        #pprint(ret[k])
        # fill 0
        if used[0][1] and used[1][1]:
            for i in range(N):
                for j in range(N):
                    if ret[k][i][j] < 0:
                        ret[k][i][j] = 0
        elif sum(used[1]) == 0:
            if len(unused[0]) == 0:
                if (sum(used[0]) == 1 and
                    (used[0][0] and uns[1][1] or used[0][1] and uns[1][0])):
                    print(-1)
                    return -1
            elif len(unused[0]) == 1:
                i, b = unused[0][0]
                for j in range(N):
                    ret[k][i][j] = b
                count = 0
                for j, jb in unused[1]:
                    if jb != b:
                        ret[k][i][j] = jb
                        count += 1
                if count == N:
                    print(-1)
                    return -1
            else:
                for j in range(N):
                    idx = j % len(unused[0])
                    i, _ = unused[0][idx]
                    ret[k][i][j] = 0
        elif sum(used[0]) == 0:
            if len(unused[1]) == 0:
                if (sum(used[1]) == 1 and
                    (used[1][0] and uns[0][1] or used[1][1] and uns[0][0])):
                    print(-1)
                    return -1
            elif len(unused[1]) == 1:
                j, b = unused[1][0]
                for i in range(N):
                    ret[k][i][j] = b
                count = 0
                for i, ib in unused[0]:
                    if ib != b:
                        ret[k][i][j] = ib
                        count += 1
                if count == N:
                    print(-1)
                    return -1
            else:
                for i in range(N):
                    idx = i % len(unused[1])
                    j, _ = unused[1][idx]
                    ret[k][i][j] = 0

#pprint(ret[k])
        # fill 0
        if used[0][1] and used[1][1]:
            for i in range(N):
                for j in range(N):
                    if ret[k][i][j] < 0:
                        ret[k][i][j] = 0
        elif sum(used[1]) == 0:
            if len(unused[0]) == 0:
                if (sum(used[0]) == 1 and
                    (used[0][0] and uns[1][1] or used[0][1] and uns[1][0])):
                    print(-1)
                    return -1
            elif len(unused[0]) == 1:
                i, b = unused[0][0]
                for j in range(N):
                    ret[k][i][j] = b
                count = 0
                for j, jb in unused[1]:
                    if jb != b:
                        ret[k][i][j] = jb
                        count += 1
                if count == N:
                    print(-1)
                    return -1
            else:
                for j in range(N):
                    idx = j % len(unused[0])
                    i, _ = unused[0][idx]
                    ret[k][i][j] = 0
        elif sum(used[0]) == 0:
            if len(unused[1]) == 0:
                if (sum(used[1]) == 1 and
                    (used[1][0] and uns[0][1] or used[1][1] and uns[0][0])):
                    print(-1)
                    return -1
            elif len(unused[1]) == 1:
                j, b = unused[1][0]
                for i in range(N):
                    ret[k][i][j] = b
                count = 0
                for i, ib in unused[0]:
                    if ib != b:
                        ret[k][i][j] = ib
                        count += 1
                if count == N:
                    print(-1)
                    return -1
            else:
                for i in range(N):
                    idx = i % len(unused[1])
                    j, _ = unused[1][idx]
                    ret[k][i][j] = 0

        #pprint(ret[k])
        #print(' -- ')
        for i in range(N):
            for j in range(N):
                if ret[k][i][j] < 0:
                    ret[k][i][j] = 1


    #pprint(ret)
    for i in range(N):
        row = []
        for j in range(N):
            tmp = 0
            for k in range(bits):
                tmp += (ret[k][i][j] << k)
            row.append(tmp)
        print(' '.join([str(r) for r in row]))

    return

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
print(-1)

Submission status: Wrong Answer

Solution code:
#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

N = int(input())

S = list(map(int,input().split()))
T = list(map(int,input().split()))
U = list(map(int,input().split()))
V = list(map(int,input().split()))

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

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

for n in range(1):
  u = [(i>>n)&1 for i in U]
  v = [(i>>n)&1 for i in V]
  b = 0
  check = 0
  ###行のAND=1とOR=0を埋める###
  for i in range(N):
    if S[i] != u[i]:
      if u[i] == 1:
        b |= mask_gyo[i]
      check |= mask_gyo[i]
  ###列のAND=1とOR=0を埋める###
  for i in range(N):
    if T[i] != v[i]:
      if v[i] == 1:
        if b & mask_retu[i] != check & mask_retu[i]:
          print(-1)
          exit()
        b |= mask_retu[i]
      if v[i] == 0:
        if (b & mask_retu[i]) ^ (check & mask_retu[i]) != (check & mask_retu[i]):
          print(-1)
          exit()
      check |= mask_retu[i]

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

  for i in range(N):
    if S[i] == 0 and u[i] == 0:
      if b & mask_gyo[i] == mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 0 and u[i] == 1:
      if b & mask_gyo[i] != mask_gyo[i]:
        print(-1)
        exit()
    if S[i] == 1 and u[i] == 0:
      if b & mask_gyo[i] != 0:
        print(-1)
        exit()
    if S[i] == 1 and u[i] == 1:
      if b & mask_gyo[i] == 0:
        print(-1)
        exit()
  
  for i in range(N):
    if T[i] == 0 and v[i] == 0:
      if b & mask_retu[i] == mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 0 and v[i] == 1:
      if b & mask_retu[i] != mask_retu[i]:
        print(-1)
        exit()
    if T[i] == 1 and v[i] == 0:
      if b & mask_retu[i] != 0:
        print(-1)
        exit()
    if T[i] == 1 and v[i] == 1:
      if b & mask_retu[i] == 0:
        print(-1)
        exit()
        
  
  for j in range(N):
    for i in range(N):
      digit = (1<<i)<<(N*j)
      a = not (b & digit == 0)
      res[j][i] += a<<n

  
for item in res:
  print(*item)