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
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N = ir()
    S = lr()
    T = lr()
    U = lr()
    V = lr()
    # d = [[-1]*N for i in range(N)]
    # def flip():
    #     for i in range(N):
    #         for j in range(i):
    #             d[i][j], d[j][i] = d[j][i], d[i][j]
    a = [[0]*N for i in range(N)]
    for k in range(64):
        val = [[(U[i]>>k)&1, (V[i]>>k)&1] for i in range(N)]
        d = [[-1]*N for i in range(N)]
        # for b in range(2):
        for i in range(N):
            x = val[i][0]
            if S[i] != x:
                for j in range(N):
                    if d[i][j] != x and d[i][j] != -1:
                        print(-1)
                        return
                    d[i][j] = x
        for i in range(N):
            x = val[i][1]
            if T[i] != x:
                for j in range(N):
                    if d[j][i] != x and d[j][i] != -1:
                        print(-1)
                        return
                    d[j][i] = x
            # flip()
        for _ in range(2):
            # for b in range(2):
            for i in range(N):
                x = val[i][0]
                if S[i] == x:
                    p = []
                    ok = False
                    for j in range(N):
                        if d[i][j] == -1:
                            p.append(j)
                        if d[i][j] == x:
                            ok = True
                    if ok:
                        continue
                    if len(p) == 0:
                        print(-1)
                        return
                    if len(p) == 1:
                        d[i][p[0]] = x
            for i in range(N):
                x = val[i][1]
                if T[i] == x:
                    p = []
                    ok = False
                    for j in range(N):
                        if d[j][i] == -1:
                            p.append(i)
                        if d[j][i] == x:
                            ok = True
                    if ok:
                        continue
                    if len(p) == 0:
                        print(-1)
                        return
                    if len(p) == 1:
                        d[p[0]][i] = x
                # flip()
        ni = []
        nj = []
        for i in range(N):
            filled = True
            for j in range(N):
                if d[i][j] == -1:
                    filled = False
                    break
            if not filled:
                ni.append(i)
        for j in range(N):
            filled = True
            for i in range(N):
                if d[i][j] == -1:
                    filled = False
                    break
            if not filled:
                nj.append(j)
        for i, ii in enumerate(ni):
            for j, jj in enumerate(nj):
                d[ii][jj] = (i+j)%2
        for i in range(N):
            for j in range(N):
                a[i][j] |= d[i][j]<<k
    for i in range(N):
        print(*a[i], sep=' ')
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N = ir()
    S = lr()
    T = lr()
    U = lr()
    V = lr()
    s = [[S[i], T[i]] for i in range(N)]
    d = [[-1]*N for i in range(N)]
    # def flip():
    #     for i in range(N):
    #         for j in range(i):
    #             d[i][j], d[j][i] = d[j][i], d[i][j]
    a = [[0]*N for i in range(N)]
    for k in range(64):
        val = [[bool(U[i]&(1<<k)), bool(V[i]&(1<<k))] for i in range(N)]
        d = [[-1]*N for i in range(N)]
        # for b in range(2):
        for i in range(N):
            x = val[i][0]
            if s[i][0] != x:
                for j in range(N):
                    if d[i][j] != x and d[i][j] != -1:
                        print(-1)
                        return
                    d[i][j] = x
        for i in range(N):
            x = val[i][1]
            if s[i][1] != x:
                for j in range(N):
                    if d[j][i] != x and d[j][i] != -1:
                        print(-1)
                        return
                    d[j][i] = x
            # flip()
        for _ in range(2):
            # for b in range(2):
            for i in range(N):
                x = val[i][0]
                if s[i][0] == x:
                    p = []
                    ok = False
                    for j in range(N):
                        if d[i][j] == -1:
                            p.append(j)
                        if d[i][j] == x:
                            ok = True
                    if ok:
                        continue
                    if len(p) == 0:
                        print(-1)
                        return
                    if len(p) == 1:
                        d[i][p[0]] = x
            for i in range(N):
                x = val[i][1]
                if s[i][1] == x:
                    p = []
                    ok = False
                    for j in range(N):
                        if d[j][i] == -1:
                            p.append(i)
                        if d[j][i] == x:
                            ok = True
                    if ok:
                        continue
                    if len(p) == 0:
                        print(-1)
                        return
                    if len(p) == 1:
                        d[p[0]][i] = x
                # flip()
        ni = []
        nj = []
        for i in range(N):
            if -1 in d[i]:
                ni.append(i)
        for j in range(N):
            filled = True
            for i in range(N):
                if d[i][j] == -1:
                    filled = False
                    break
            if not filled:
                nj.append(j)
        for i in ni:
            for j in nj:
                d[i][j] = (i+j)%2
        for i in range(N):
            for j in range(N):
                a[i][j] |= d[i][j]<<k
    for i in range(N):
        print(*a[i], sep=' ')
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N = ir()
    S = lr()
    T = lr()
    U = lr()
    V = lr()
    s = [[S[i], T[i]] for i in range(N)]
    def flip(td):
        for i in range(N):
            for j in range(i):
                td[i][j], td[j][i] = td[j][i], td[i][j]
        return td
    a = [[0]*N for i in range(N)]
    for k in range(64):
        val = [[U[i]&(1<<k), V[i]&(1<<k)] for i in range(N)]
        d = [[-1]*N for i in range(N)]
        for b in range(2):
            for i in range(N):
                x = val[i][b]
                if s[i][b] != x:
                    for j in range(N):
                        if d[i][j] != x and d[i][j] != -1:
                            print(-1)
                            return
                        d[i][j] = x
            d = flip(d)
        for _ in range(2):
            for b in range(2):
                for i in range(N):
                    x = val[i][b]
                    if s[i][b] == x:
                        p = []
                        ok = False
                        for j in range(N):
                            if d[i][j] == -1:
                                p.append(j)
                            if d[i][j] == x:
                                ok = True
                        if ok:
                            continue
                        if len(p) == 0:
                            print(-1)
                            return
                        if len(p) == 1:
                            d[i][p[0]] = x
                d = flip(d)
        ni = []
        nj = []
        for i in range(N):
            filled = True
            for j in range(N):
                if d[i][j] == -1:
                    filled = False
            if not filled:
                ni.append(i)
        for j in range(N):
            filled = True
            for i in range(N):
                if d[i][j] == -1:
                    filled = False
            if not filled:
                nj.append(j)
        for i in ni:
            for j in nj:
                d[i][j] = (i+j)%2
        for i in range(N):
            for j in range(N):
                a[i][j] |= d[i][j]<<k
    for i in range(N):
        print(*a[i], sep=' ')
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N = ir()
    S = lr()
    T = lr()
    U = lr()
    V = lr()
    a = [[0]*N for i in range(N)]
    f = [[0]*N for i in range(N)]
    #少なくとも1つ以上1が入るが確定していない行のindex
    row = [[] for i in range(64)]
    #少なくとも1つ以上1が入るが確定していない列のindex
    col = [[] for i in range(64)]
    for i in range(N):
        for j in range(N):
            s = S[i]
            t = T[j]
            for k in range(64):
                u = (U[i]>>k)&1
                v = (V[j]>>k)&1
                if u == v:
                    f[i][j] += 1<<k
                    if u == 1:
                        a[i][j] += 1<<k
                    continue
                if s == 0 and u == 1:
                    if t == 1 and v == 0:
                        print(-1)
                        return
                    f[i][j] += 1<<k
                    a[i][j] += 1<<k
                    continue
                elif s == 1 and u == 0:
                    if t == 0 and v == 1:
                        print(-1)
                        return
                    f[i][j] += 1<<k
                    continue
                else:
                    if t == 0 and v == 1:
                        f[i][j] += 1<<k
                        a[i][j] += 1<<k
                        continue
                    elif t == 1 and v == 0:
                        f[i][j] += 1<<k
                        continue
                if u == 1:
                    row[k].append(i)
                if v == 1:
                    col[k].append(j)
    nrow = [[] for i in range(64)]
    ncol = [[] for i in range(64)]
    for k in range(64):
        while len(row[k]) > 0:
            i = row[k].pop()
            tmp = []
            for j in range(N):
                if a[i][j]>>k&1 == 1:
                    break
                else:
                    if f[i][j]>>k&1 == 0:
                        tmp.append(j)
            else:
                if len(tmp) > 1:
                    nrow[k].append(i)
                elif len(tmp) == 1:
                    a[i][tmp[0]] += 1<<k
                else:
                    print(-1)
                    return
    for k in range(64):
        while len(col[k]) > 0:
            j = col[k].pop()
            tmp = []
            for i in range(N):
                if a[i][j]>>k&1 == 1:
                    break
                else:
                    if f[i][j]>>k&1 == 0:
                        tmp.append(i)
            else:
                if len(tmp) > 1:
                    ncol[k].append(j)
                elif len(tmp) == 1:
                    f[tmp[0]][j] += 1<<k
                    a[tmp[0]][j] += 1<<k
                else:
                    print(-1)
                    return
    for k in range(64):
        tmp = 0
        for i in nrow[k]:
            for j in ncol[k]:
                if tmp == 1:
                    a[i][j] += 1<<k
                tmp = (tmp+1)%2
    for i in range(N):
        print(*a[i], sep=' ')
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import deque
def resolve():
    N = ir()
    S = lr()
    T = lr()
    U = lr()
    V = lr()
    a = [[0]*N for i in range(N)]
    f = [[0]*N for i in range(N)]
    #少なくとも1つ以上1が入るが確定していない行のindex
    row = [deque([]) for i in range(64)]
    #少なくとも1つ以上1が入るが確定していない列のindex
    col = [deque([]) for i in range(64)]
    for i in range(N):
        for j in range(N):
            s = S[i]
            t = T[j]
            for k in range(64):
                u = (U[i]>>k)&1
                v = (V[j]>>k)&1
                if u == v:
                    f[i][j] += 1<<k
                    if u == 1:
                        a[i][j] += 1<<k
                    continue
                if s == 0 and u == 1:
                    if t == 1 and v == 0:
                        print(-1)
                        return
                    f[i][j] += 1<<k
                    a[i][j] += 1<<k
                    continue
                elif s == 1 and u == 0:
                    if t == 0 and v == 1:
                        print(-1)
                        return
                    f[i][j] += 1<<k
                    continue
                else:
                    if t == 0 and v == 1:
                        f[i][j] += 1<<k
                        a[i][j] += 1<<k
                        continue
                    elif t == 1 and v == 0:
                        f[i][j] += 1<<k
                        continue
                if u == 1:
                    row[k].append(i)
                if v == 1:
                    col[k].append(j)
    for k in range(64):
        while len(row[k]) > 0:
            i = row[k].popleft()
            tmp = []
            for j in range(N):
                if a[i][j]>>k&1 == 1:
                    break
                else:
                    if f[i][j]>>k&1 == 0:
                        tmp.append(j)
            else:
                if len(tmp) > 1:
                    row[k].append(i)
                elif len(tmp) == 1:
                    a[i][tmp[0]] += 1<<k
                else:
                    print(-1)
                    return
    for k in range(64):
        while len(col[k]) > 0:
            j = col[k].popleft()
            tmp = []
            for i in range(N):
                if a[i][j]>>k&1 == 1:
                    break
                else:
                    if f[i][j]>>k&1 == 0:
                        tmp.append(i)
            else:
                if len(tmp) > 1:
                    col[k].append(j)
                elif len(tmp) == 1:
                    a[tmp[0]][j] += 1<<k
                else:
                    print(-1)
                    return
    for i in range(N):
        print(*a[i], sep=' ')
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N = ir()
    S = lr()
    T = lr()
    U = lr()
    V = lr()
    a = [[0]*N for i in range(N)]
    b = [[-2]*64 for i in range(N)]
    c = [[-2]*64 for i in range(N)]
    for i in range(N):
        for j in range(N):
            s = S[i]
            t = T[j]
            for k in range(64):
                u = (U[i]>>k)&1
                if b[i][k] != -1:
                    b[i][k] = u
                v = (V[j]>>k)&1
                if c[j][k] != -1:
                    c[j][k] = v
                if s == 0 and u == 1:
                    if t == 1 and v == 0:
                        print(-1)
                        return
                    a[i][j] += 1<<k
                    if b[i][k] == 1:
                        b[i][k] = -1
                    if c[j][k] == 1:
                        c[j][k] = -1
                elif s == 1 and u == 0:
                    if t == 0 and v == 1:
                        print(-1)
                        return
                    if b[i][k] == 0:
                        b[i][k] = -1
                    if c[j][k] == 0:
                        c[j][k] = -1
                elif s == 0 and u == 0:
                    if t == 0 and v == 1:
                        a[i][j] += 1<<k
                        if b[i][k] == 1:
                            b[i][k] = -1
                        if c[j][k] == 1:
                            c[j][k] = -1
                    elif t == 1 and v == 0:
                        if b[i][k] == 0:
                            b[i][k] = -1
                        if c[j][k] == 0:
                            c[j][k] = -1
                    else:
                        if b[i][k] == 0:
                            b[i][k] = -1
                            if c[j][k] == 0:
                                c[j][k] = -1
                        elif b[i][k] == 1:
                            a[i][j] += 1<<k
                            b[i][k] = -1
                            if c[j][k] == 1:
                                c[j][k] = -1
                        else:
                            if c[j][k] == 1:
                                a[i][j] += 1<<k
                            c[j][k] = -1
                else:
                    if t == 0 and v == 1:
                        a[i][j] += 1<<k
                        if b[i][k] == 1:
                            b[i][k] = -1
                        if c[j][k] == 1:
                            c[j][k] = -1
                    elif t == 1 and v == 0:
                        if b[i][k] == 0:
                            b[i][k] = -1
                        if c[j][k] == 0:
                            c[j][k] = -1
                    else:
                        if b[i][k] == 0:
                            b[i][k] = -1
                            if c[j][k] == 0:
                                c[j][k] = -1
                        elif b[i][k] == 1:
                            a[i][j] += 1<<k
                            b[i][k] = -1
                            if c[j][k] == 1:
                                c[j][k] = -1
                        else:
                            if c[j][k] == 1:
                                a[i][j] += 1<<k
                            c[j][k] = -1
    for i in range(N):
        if b[i] != [-1]*64 or c[i] != [-1]*64:
            print(-1)
            return
    for i in range(N):
        print(*a[i], sep=' ')
resolve()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
sysread = sys.stdin.readline
from heapq import heappop, heappush
from itertools import combinations
sys.setrecursionlimit(10**6)
import numpy as np
def run():
    N = int(sysread())
    S = list(map(int, sysread().split()))
    T = list(map(int, sysread().split()))
    U = list(map(int, sysread().split()))
    V = list(map(int, sysread().split()))

    max_n = len(bin(max(U+V))[2:])
    def to_bin(x):
        x = bin(x)[2:]
        return list("0" * (max_n - len(x)) + x)

    U = list(map(to_bin, U))
    V = list(map(to_bin, V))


    Z = np.zeros((N,N), dtype=np.uint64)
    Z_bool = np.empty((N,N,max_n))
    Z_bool[:,:,:] = False

    # fill absolutely determined columns
    row1 = np.array([[False] * max_n for _ in range(N)])
    col1 = np.array([[False] * max_n for _ in range(N)])
    row0 = np.array([[False] * max_n for _ in range(N)])
    col0 = np.array([[False] * max_n for _ in range(N)])
    row1_total = row1.sum(axis=0)
    row0_total = row0.sum(axis=0)
    col1_total = col1.sum(axis=0)
    col0_total = col0.sum(axis=0)

    for i, (s, val) in enumerate(zip(S,U)):
        if s == 0:# all 1
            for idx, k in enumerate(val):
                if k=="1":
                    Z[i,:] += 1 << (max_n-idx-1)
                    Z_bool[i,:,idx] = True
                    row1[i, idx] = True
                    row1_total[idx] += True
        else:# all 0
            for idx, k in enumerate(val):
                if k=="0":
                    Z_bool[i, :, idx] = True
                    row0[i, idx] = True
                    row0_total[idx] += True

    for j, (t, val) in enumerate(zip(T,V)):
        if t == 0:# all 1
            for idx, k in enumerate(val):
                if k=="1":
                    Z[:, j] += 1 << (max_n-idx-1)
                    Z_bool[:,j,idx] = True
                    col1[j, idx] = True
                    col1_total[idx] += True
        else:# all 0
            for idx, k in enumerate(val):
                if k=="0":
                    Z_bool[:,j,idx] = True
                    col0[j, idx] = True
                    col0_total[idx] += True

    for i in range(N):
        for idx in range(max_n):
            if (col1[i,idx] and row0_total[idx]) or (col0[i,idx] and row1_total[idx]) or \
                    (row0[i,idx] and col1_total[idx]) or (row1[i,idx] and col0_total[idx]):
                print(-1)
                return None


    # fill empty columns, according to sum-criteria
    for i, (s, val) in enumerate(zip(S,U)):
        if s == 1:# all 1
            for idx, v in enumerate(val):
                if v == '1':
                    if row1[i,idx]:continue
                    is_empty = [k for k,_v in enumerate(Z_bool[i, :, idx]) if not _v]
                    if is_empty == []:
                        print(-1)
                        return None
                    for e in is_empty:
                        if T[e] == 0:
                            if col0[e, idx]:
                                Z[i, e] += 1 << (max_n-idx-1)
                                Z_bool[i, e, idx] = True
                                col1[e,idx], row1[i,idx] = True, True
                                break
                        else:
                            Z[i, e] += 1 << (max_n - idx-1)
                            col1[e, idx], row1[i, idx] = True, True
                            Z_bool[i,e,idx] = True
                            break

    for j, (t, val) in enumerate(zip(T, V)):
        if t == 1:  # all 1
            for idx, v in enumerate(val):
                if v == '1':
                    if col1[j,idx]: continue
                    is_empty = [k for k,_v in enumerate(Z_bool[:, j, idx]) if not _v]
                    if is_empty == []:
                        print(-1)
                        return None
                    for e in is_empty:
                        if S[e] == 0:
                            if row0[e,idx]:
                                Z[e, j] += 1 << (max_n-idx-1)
                                row1[e, idx], col1[i, idx] = True, True
                                Z_bool[e,j,idx] = True
                                break
                        else:
                            Z[e, j] += 1 << (max_n - idx -1)
                            Z_bool[e,j,idx] = True
                            row1[e, idx], col1[i, idx] = True, True
                            break

    Z = Z.tolist()

    for z in Z:
        for s in z:
            print("{} ".format(s), end="")
        print("")

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
sysread = sys.stdin.readline
from heapq import heappop, heappush
from itertools import combinations
sys.setrecursionlimit(10**6)
import numpy as np
def run():
    # import sys
    # A = list(map(int, sys.stdin.readline().split()))
    # all = sys.stdin.readlines()
    #N, *A = map(int, open(0))
    #N = int(input())

    N = int(sysread())
    S = list(map(int, sysread().split()))
    T = list(map(int, sysread().split()))
    U = list(map(int, sysread().split()))
    V = list(map(int, sysread().split()))
    #=================
    """
    np.random.seed(135)
    N = 500
    S = np.random.randint(0, 1, N)
    T = np.random.randint(0, 1 ,N)
    U = np.random.randint(0, 1 << 63, N, dtype=np.uint64)
    V = np.random.randint(0, 1 << 63, N, dtype=np.uint64)
    """
    #=================
    max_n = len(bin(max(U+V))[2:])
    def to_bin(x):
        x = bin(x)[2:]
        return list("0" * (max_n - len(x)) + x)

    U = list(map(to_bin, U))
    V = list(map(to_bin, V))


    Z = np.zeros((N,N), dtype=np.uint64)
    Z_bool = np.empty((N,N,max_n))
    Z_bool[:,:,:] = False

    # fill absolutely determined columns
    row1 = np.array([[False] * max_n for _ in range(N)])
    col1 = np.array([[False] * max_n for _ in range(N)])
    row0 = np.array([[False] * max_n for _ in range(N)])
    col0 = np.array([[False] * max_n for _ in range(N)])
    row1_total = row1.sum(axis=0)
    row0_total = row0.sum(axis=0)
    col1_total = col1.sum(axis=0)
    col0_total = col0.sum(axis=0)

    for i, (s, val) in enumerate(zip(S,U)):
        if s == 0:# all 1
            for idx, k in enumerate(val):
                if k=="1":
                    Z[i,:] += 1 << (max_n-idx-1)
                    Z_bool[i,:,idx] = True
                    row1[i, idx] = True
                    row1_total[idx] += True
        else:# all 0
            for idx, k in enumerate(val):
                if k=="0":
                    Z_bool[i, :, idx] = True
                    row0[i, idx] = True
                    row0_total[idx] += True

    for j, (t, val) in enumerate(zip(T,V)):
        if t == 0:# all 1
            for idx, k in enumerate(val):
                if k=="1":
                    Z[:, j] += 1 << (max_n-idx-1)
                    Z_bool[:,j,idx] = True
                    col1[j, idx] = True
                    col1_total[idx] += True
        else:# all 0
            for idx, k in enumerate(val):
                if k=="0":
                    Z_bool[:,j,idx] = True
                    col0[j, idx] = True
                    col0_total[idx] += True

    for i in range(N):
        for idx in range(max_n):
            if (col1[i,idx] and row0_total[idx]) or col0[i,idx] and row1_total[idx] or \
                    row0[i,idx] and col1_total[idx] or row1[i,idx] and col0_total[idx]:
                print(-1)
                return None


    # fill empty columns, according to sum-criteria
    for i, (s, val) in enumerate(zip(S,U)):
        if s == 1:# all 1
            for idx, v in enumerate(val):
                if v == '1':
                    if row1[i,idx]:continue
                    is_empty = [k for k,_v in enumerate(Z_bool[i, :, idx]) if not _v]
                    if is_empty == []:
                        print(-1)
                        return None
                    for e in is_empty:
                        if T[e] == 0:
                            if col0[e, idx]:
                                Z[i, e] += 1 << (max_n-idx-1)
                                Z_bool[i, e, idx] = True
                                col1[e,idx], row1[i,idx] = True, True
                                break
                        else:
                            Z[i, e] += 1 << (max_n - idx-1)
                            col1[e, idx], row1[i, idx] = True, True
                            Z_bool[i,e,idx] = True

                            break

    for j, (t, val) in enumerate(zip(T, V)):
        if t == 1:  # all 1
            for idx, v in enumerate(val):
                if v == '1':
                    if col1[j,idx]: continue
                    is_empty = [k for k,_v in enumerate(Z_bool[:, j, idx]) if not _v]
                    if is_empty == []:
                        print(-1)
                        return None
                    for e in is_empty:
                        if S[e] == 0:
                            if row0[e,idx]:
                                Z[e, j] += 1 << (max_n-idx-1)
                                row1[e, idx], col1[i, idx] = True, True
                                Z_bool[e,j,idx] = True
                                break
                        else:
                            Z[e, j] += 1 << (max_n - idx -1)
                            Z_bool[e,j,idx] = True
                            row1[e, idx], col1[i, idx] = True, True
                            break

    Z = Z.tolist()

    for z in Z:
        for s in z:
            print("{} ".format(s), end="")
        print("")

if __name__ == "__main__":
    run()