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:
def add_row(ans, n, i, k):
    for j in range(n):
        ans[i][j] += k


def add_column(ans, n, j, k):
    for i in range(n):
        ans[i][j] += k


def solve(n, sss, ttt, uuu, vvv):
    max_d = max(max(uuu).bit_length(), max(vvv).bit_length())
    ans = [[0] * n for _ in range(n)]

    for d in range(max_d):
        k = 1 << d
        ambiguous_i = []
        ambiguous_j = []
        filled_i = [False, False]
        filled_j = [False, False]
        for i, (s, u) in enumerate(zip(sss, uuu)):
            if s == 0:
                if u & k:
                    add_row(ans, n, i, k)
                    filled_i[1] = True
                else:
                    ambiguous_i.append((i, 0))
            else:
                if u & k:
                    ambiguous_i.append((i, 1))
                else:
                    filled_i[0] = True
        for j, (t, v) in enumerate(zip(ttt, vvv)):
            if t == 0:
                if v & k:
                    if filled_i[0]:
                        return -1
                    add_column(ans, n, j, k)
                    filled_j[1] = True
                else:
                    ambiguous_j.append((j, 0))
            else:
                if v & k:
                    ambiguous_j.append((j, 1))
                else:
                    if filled_i[1]:
                        return -1
                    filled_j[0] = True

        # print(d)
        # print(ans)
        # print(ambiguous_i)
        # print(ambiguous_j)

        if len(ambiguous_i) == 0:
            amb = set(bj for j, bj in ambiguous_j)
            for bj in amb:
                if not filled_i[bj]:
                    return -1
        elif len(ambiguous_j) == 0:
            amb = set(bi for i, bi in ambiguous_i)
            for bi in amb:
                if not filled_j[bi]:
                    return -1
        elif len(ambiguous_i) == 1:
            i, bi = ambiguous_i[0]
            if filled_i[bi ^ 1]:
                if bi == 1:
                    add_row(ans, n, i, k)
            else:
                ok = False
                for j, bj in ambiguous_j:
                    if bj:
                        ans[i][j] += k
                    if bi == bj:
                        ok = True
                if not ok:
                    return -1
        elif len(ambiguous_j) == 1:
            j, bj = ambiguous_j[0]
            if filled_j[bj ^ 1]:
                if bj == 1:
                    add_column(ans, n, j, k)
            else:
                ok = False
                for i, bi in ambiguous_i:
                    if bi:
                        ans[i][j] += k
                    if bi == bj:
                        ok = True
                if not ok:
                    return -1
        else:
            for ii, (i, bi) in enumerate(ambiguous_i):
                for jj, (j, bj) in enumerate(ambiguous_j):
                    if (ii ^ jj) & 1:
                        ans[i][j] += k

    return ans


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

ans = solve(n, sss, ttt, uuu, vvv)
if ans == -1:
    print(ans)
else:
    for row in ans:
        print(*row)

Submission status: Wrong Answer

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

def main():
    N = int(readline())
    S = list(map(int,readline().split()))
    T = list(map(int,readline().split()))
    U = list(map(int,readline().split()))
    V = list(map(int,readline().split()))
    ans = [[''] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if S[i] == T[j]:
                ans[i][j] = str(U[i] | V[j])
            else:
                ans[i][j] = str(U[i] & V[j])
    for i in range(N):
        print(' '.join(ans[i]))
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np


def solve(n, s_list, t_list, u_list, v_list):

    x = np.zeros((n, n, 64), dtype=np.uint64)

    for k in range(64):

        # mod
        uk_list = [(u // (2 ** k)) % 2 for u in u_list]
        vk_list = [(v // (2 ** k)) % 2 for v in v_list]

        # count
        all_1_row = 0
        not_all_1_row = 0
        all_0_row = 0
        not_all_0_row = 0
        all_1_col = 0
        not_all_1_col = 0
        all_0_col = 0
        not_all_0_col = 0

        # row
        for i in range(n):
            if s_list[i] == 0 and uk_list[i] == 1:
                # all 1
                all_1_row += 1
            elif s_list[i] == 0 and uk_list[i] == 0:
                # not all 1
                not_all_1_row += 1
            elif s_list[i] == 1 and uk_list[i] == 0:
                # all 0
                all_0_row += 1
            else:
                # not all 0
                not_all_0_row += 1
        # col
        for j in range(n):
            if t_list[j] == 0 and vk_list[j] == 1:
                # all 1
                all_1_col += 1
            elif t_list[j] == 0 and vk_list[j] == 0:
                # not all 1
                not_all_1_col += 1
            elif t_list[j] == 1 and vk_list[j] == 0:
                # all 0
                all_0_col += 1
            else:
                # not all 0
                not_all_0_col += 1

        # impossible
        if all_1_row > 0 and all_0_col > 0:
            return [-1]
        elif all_0_row > 0 and all_1_col > 0:
            return [-1]
        elif all_1_col == n and not_all_1_row > 0:
            return [-1]
        elif all_0_col == n and not_all_0_row > 0:
            return [-1]
        elif all_1_row == n and not_all_1_col > 0:
            return [-1]
        elif all_0_row == n and not_all_0_col > 0:
            return [-1]

        # all 1 in both
        if all_1_row > 0 and all_1_col > 0:
            for i in range(n):
                # all 1
                if s_list[i] == 0 and uk_list[i] == 1:
                    x[i, :, k] = 1
            for j in range(n):
                # all 1
                if t_list[j] == 0 and vk_list[j] == 1:
                    x[:, j, k] = 1

        # all 0 in both
        elif all_0_row > 0 and all_0_col > 0:
            x[:, :, k] = 1
            for i in range(n):
                # all 0
                if s_list[i] == 1 and uk_list[i] == 0:
                    x[i, :, k] = 0
            for j in range(n):
                # all 0
                if t_list[j] == 1 and vk_list[j] == 0:
                    x[:, j, k] = 0

        # only row
        elif all_0_col == 0 and all_1_col == 0:
            if sum(uk_list) == n:
                x[:, :, k] = 1
                if all_1_row == n - 1:
                    if not_all_1_col == n:
                        return [-1]
                    else:
                        for i in range(n):
                            if s_list[i] == 1:
                                for j in range(n):
                                    if t_list[j] == 0 and vk_list[j] == 0:
                                        x[i, j, k] = 0
                else:
                    cnt = 0
                    for i in range(n):
                        if s_list[i] == 1:
                            if cnt == 0:
                                x[i, 0::2, k] = 0
                            elif cnt == 1:
                                x[i, 1::2, k] = 0
                            cnt += 1

            elif sum(uk_list) == 0:
                x[:, :, k] = 0
                if all_0_row == n - 1:
                    if not_all_1_col == n:
                        return [-1]
                    else:
                        for i in range(n):
                            if s_list[i] == 0:
                                for j in range(n):
                                    if t_list[j] == 1 and vk_list[j] == 1:
                                        x[i, j, k] = 1
                else:
                    cnt = 0
                    for i in range(n):
                        if s_list[i] == 0:
                            if cnt == 0:
                                x[i, 0::2, k] = 1
                            elif cnt == 1:
                                x[i, 1::2, k] = 1
                            cnt += 1
            else:
                for i in range(n):
                    x[i, :, k] = uk_list[i]
        # only col
        else:
            if sum(vk_list) == n:
                x[:, :, k] = 1
                if all_1_col == n - 1:
                    if not_all_1_row == n:
                        return [-1]
                    else:
                        for j in range(n):
                            if t_list[j] == 1:
                                for i in range(n):
                                    if s_list[i] == 0 and uk_list[i] == 0:
                                        x[i, j, k] = 0
                else:
                    cnt = 0
                    for j in range(n):
                        if t_list[j] == 1:
                            if cnt == 0:
                                x[0::2, j, k] = 0
                            elif cnt == 1:
                                x[1::2, j, k] = 0
                            cnt += 1

            elif sum(vk_list) == 0:
                x[:, :, k] = 0
                if all_0_col == n - 1:
                    if not_all_1_row == n:
                        return [-1]
                    else:
                        for j in range(n):
                            if t_list[j] == 0:
                                for i in range(n):
                                    if s_list[i] == 1 and uk_list[i] == 1:
                                        x[i, j, k] = 1
                else:
                    cnt = 0
                    for j in range(n):
                        if t_list[j] == 0:
                            if cnt == 0:
                                x[0::2, j, k] = 1
                            elif cnt == 1:
                                x[1::2, j, k] = 1
                            cnt += 1
            else:
                for j in range(n):
                    x[:, j, k] = vk_list[j]

    for k in range(64):
        x[:, :, k] = x[:, :, k] * (2 ** k)

    y = x.sum(axis=2)
    res = []
    for i in range(n):
        res.append(" ".join([str(z) for z in y[i, :]]))

    # print(res)

    return res


def main():
    n = int(input())
    s_list = list(map(int, input().split()))
    t_list = list(map(int, input().split()))
    u_list = list(map(int, input().split()))
    v_list = list(map(int, input().split()))
    res = solve(n, s_list, t_list, u_list, v_list)
    for r in res:
        print(r)


def test():
    assert solve(2, [0, 1], [1, 0], [1, 1], [1, 0]) == 0
    assert solve(2, [1, 1], [1, 0], [15, 15], [15, 11]) == 0


if __name__ == "__main__":
    # test()
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np


def solve(n, s_list, t_list, u_list, v_list):

    x = np.zeros((n, n, 64), dtype=np.uint64)

    for k in range(64):

        # mod
        uk_list = [(u // (2 ** k)) % 2 for u in u_list]
        vk_list = [(v // (2 ** k)) % 2 for v in v_list]

        # count
        all_1_row = 0
        not_all_1_row = 0
        all_0_row = 0
        not_all_0_row = 0
        all_1_col = 0
        not_all_1_col = 0
        all_0_col = 0
        not_all_0_col = 0

        # row
        for i in range(n):
            if s_list[i] == 0 and uk_list[i] == 1:
                # all 1
                all_1_row += 1
            elif s_list[i] == 0 and uk_list[i] == 0:
                # not all 1
                not_all_1_row += 1
            elif s_list[i] == 1 and uk_list[i] == 0:
                # all 0
                all_0_row += 1
            else:
                # not all 0
                not_all_0_row += 1
        # col
        for j in range(n):
            if t_list[j] == 0 and vk_list[j] == 1:
                # all 1
                all_1_col += 1
            elif t_list[j] == 0 and vk_list[j] == 0:
                # not all 1
                not_all_1_col += 1
            elif t_list[j] == 1 and vk_list[j] == 0:
                # all 0
                all_0_col += 1
            else:
                # not all 0
                not_all_0_col += 1

        # impossible
        if all_1_row > 0 and all_0_col > 0:
            return [-1]
        elif all_0_row > 0 and all_1_col > 0:
            return [-1]
        elif all_1_col == n and not_all_1_row > 0:
            return [-1]
        elif all_0_col == n and not_all_0_row > 0:
            return [-1]
        elif all_1_row == n and not_all_1_col > 0:
            return [-1]
        elif all_0_row == n and not_all_0_col > 0:
            return [-1]

        # all 1 in both
        if all_1_row > 0 and all_1_col > 0:
            for i in range(n):
                # all 1
                if s_list[i] == 0 and uk_list[i] == 1:
                    x[i, :, k] = 1
            for j in range(n):
                # all 1
                if t_list[j] == 0 and vk_list[j] == 1:
                    x[:, j, k] = 1

        # all 0 in both
        elif all_0_row > 0 and all_0_col > 0:
            x[:, :, k] = 1
            for i in range(n):
                # all 0
                if s_list[i] == 1 and uk_list[i] == 0:
                    x[i, :, k] = 0
            for j in range(n):
                # all 0
                if t_list[j] == 1 and vk_list[j] == 0:
                    x[:, j, k] = 0

        # only row
        elif all_0_col == 0 and all_1_col == 0:
            if sum(uk_list) == n:
                x[:, :, k] = 1
                if all_1_row == n - 1:
                    if not_all_1_col == n:
                        return [-1]
                    else:
                        for i in range(n):
                            if s_list[i] == 1:
                                for j in range(n):
                                    if t_list[j] == 0 and vk_list[j] == 0:
                                        x[i, j, k] = 0
                else:
                    cnt = 0
                    for i in range(n):
                        if s_list[i] == 1:
                            if cnt == 0:
                                x[i, 0::2, k] = 0
                            elif cnt == 1:
                                x[i, 1::2, k] = 0
                            cnt += 1

            elif sum(uk_list) == 0:
                x[:, :, k] = 0
                if all_0_row == n - 1:
                    if not_all_1_col == n:
                        return [-1]
                    else:
                        for i in range(n):
                            if s_list[i] == 0:
                                for j in range(n):
                                    if t_list[j] == 1 and vk_list[j] == 1:
                                        x[i, j, k] = 1
                else:
                    cnt = 0
                    for i in range(n):
                        if s_list[i] == 0:
                            if cnt == 0:
                                x[i, 0::2, k] = 1
                            elif cnt == 1:
                                x[i, 1::2, k] = 1
                            cnt += 1
            else:
                for i in range(n):
                    x[i, :, k] = uk_list[i]
        # only col
        else:
            if sum(vk_list) == n:
                x[:, :, k] = 1
                if all_1_col == n - 1:
                    if not_all_1_row == n:
                        return [-1]
                    else:
                        for j in range(n):
                            if t_list[j] == 1:
                                for i in range(n):
                                    if s_list[i] == 0 and uk_list[i] == 0:
                                        x[i, j, k] = 0
                else:
                    cnt = 0
                    for j in range(n):
                        if t_list[j] == 1:
                            if cnt == 0:
                                x[0::2, j, k] = 0
                            elif cnt == 1:
                                x[1::2, j, k] = 0
                            cnt += 1

            elif sum(vk_list) == 0:
                x[:, :, k] = 0
                if all_0_col == n - 1:
                    if not_all_1_row == n:
                        return [-1]
                    else:
                        for j in range(n):
                            if t_list[j] == 0:
                                for i in range(n):
                                    if s_list[i] == 1 and uk_list[i] == 1:
                                        x[i, j, k] = 1
                else:
                    cnt = 0
                    for j in range(n):
                        if t_list[j] == 0:
                            if cnt == 0:
                                x[0::2, j, k] = 1
                            elif cnt == 1:
                                x[1::2, j, k] = 1
                            cnt += 1
            else:
                for j in range(n):
                    x[:, j, k] = vk_list[j]

    for k in range(64):
        x[:, :, k] = x[:, :, k] * (2 ** k)

    y = x.sum(axis=2)
    res = []
    for i in range(n):
        res.append(" ".join([str(z) for z in y[i, :]]))

    print(res)

    return res


def main():
    n = int(input())
    s_list = list(map(int, input().split()))
    t_list = list(map(int, input().split()))
    u_list = list(map(int, input().split()))
    v_list = list(map(int, input().split()))
    res = solve(n, s_list, t_list, u_list, v_list)
    for r in res:
        print(r)


def test():
    assert solve(2, [0, 1], [1, 0], [1, 1], [1, 0]) == 0
    assert solve(2, [1, 1], [1, 0], [15, 15], [15, 11]) == 0


if __name__ == "__main__":
    # test()
    main()

Submission status: Wrong Answer

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

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


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


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


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

if not test(S, T, U, V, A):
    print(-1)
else:
    print('\n'.join(' '.join(row) for row in A.astype(str)))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(map(int,input().split()))
t=list(map(int,input().split()))
u=list(map(int,input().split()))
v=list(map(int,input().split()))
matrix=[[0 for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        if s[i]==0 and t[j]==0:
            matrix[i][j]=u[i]|v[j]
        elif s[i]==0 and t[j]==1:
            if u[i]|v[j]!=v[j]:print(-1);exit()
            matrix[i][j]=v[j]
        elif s[i]==1 and t[j]==0:
            if u[i]|v[j]!=u[i]:print(-1);exit()
            matrix[i][j]=u[i]
        else:
            matrix[i][j]=u[i] & v[j]
for i in matrix:print(*i)

Submission status: Wrong Answer

Solution code:
N = str(input())
N_len = len(N)
cnt = 0

t_list = []
tmp = 1

while tmp < 100:
    t_list.append(str(2019*tmp))
    tmp += 1

for t in t_list:
    for ind in range(len(N)-len(t)+1):
        if N[ind:ind+len(t)] == t:
            print(t)
            cnt += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
yr = list(map(int, input().split()))
tr = list(map(int, input().split()))
yb = list(map(int, input().split()))
tb = list(map(int, input().split()))

jud = True
ansl = [[0 for _ in range(n)] for _ in range(n)]
for k in range(64):
	finall = [[-1 for _ in range(n)] for _ in range(n)]
	yzerocnter = [0 for _ in range(n)]
	yonecnter = [0 for _ in range(n)]
	tzerocnter = [0 for _ in range(n)]
	tonecnter = [0 for _ in range(n)]
	for i in range(n):
		if (yr[i]==0)&((yb[i]>>k)%2==1):
			for j in range(n):
				finall[i][j] = 1
				tonecnter[j] += 1
			yonecnter[i] = n
		if (yr[i]==1)&((yb[i]>>k)%2==0):
			for j in range(n):
				finall[i][j] = 0
				tzerocnter[j] += 1
			yzerocnter[i] = n
	for i in range(n):
		if (tr[i]==0)&((tb[i]>>k)%2==1):
			for j in range(n):
				if finall[j][i]==0:
					jud = False
				finall[j][i] = 1
				yonecnter[j] += 1
			tonecnter[i] = n
		if (tr[i]==1)&((tb[i]>>k)%2==0):
			for j in range(n):
				if finall[j][i]==1:
					jud = False
				finall[j][i] = 0
				yzerocnter[j] += 1
			tzerocnter[i] = n
	if jud == False:
		break
	for i in range(n):
		for j in range(n):
			if finall[i][j]==-1:
				if (yr[i]==0)&((yb[i]>>k)%2==0):
					if (tr[j]==0)&((tb[j]>>k)%2==0):
						finall[i][j] = 0
						yzerocnter[i] += 1
						tzerocnter[j] += 1
					else:
						if (yonecnter[i]==n-1)&(tzerocnter==n-1):
							jud = False 
							break
						elif yonecnter[i]>tzerocnter[j]:
							finall[i][j] = 0
							yzerocnter[i] += 1
							tzerocnter[j] += 1
						else :
							finall[i][j] = 1
							yonecnter[i] += 1
							tonecnter[j] += 1
				elif (yr[i]==1)&((yb[i]>>k)%2==1):
					if (tr[j]==0)&((tb[j]>>k)%2==0):
						if (yzerocnter[i]==n-1)&(tonecnter==n-1):
							jud = False
							break
						elif yzerocnter[i]>tonecnter[j]:
							finall[i][j] = 1
							yonecnter[i] += 1
							tonecnter[j] += 1
						else:
							finall[i][j] = 0
							yzerocnter[i] += 1
							tzerocnter[j] += 1
					else:
						finall[i][j] = 1
						yonecnter[i] += 1
						tonecnter[j] += 1
	if jud == False:
		break
	for i in range(n):
		for j in range(n):
			if finall[i][j] == 1:
				ansl[i][j] += (1<<k)


if jud == False:
	print(-1)
else:
	for i in range(n):
		anstr = ""
		for j in range(n):
			anstr += str(ansl[i][j])+" "
		print(anstr)

Submission status: Wrong Answer

Solution code:
N=int(input())
*S, = map(int,input().split())
*T, = map(int,input().split())
*U, = map(int,input().split())
*V, = map(int,input().split())

A=[[0]*N for _ in range(N)]
max_u=max([U[i] for i in range(N)])
max_v=max([V[i] for i in range(N)])
max_uv=max(max_u,max_v)

def row_bit(A,S,U):
  for i in range(N):
    if S[i]==0:
      for j in range(N):
          if A[i][j]&A[i][j]==U[i]:
              continue
          else:
              return False
    elif S[i]==1:
       for j in range(N):
          if A[i][j]|A[i][j]==U[i]:
             continue
          else:
             return False
            
def col_bit(A,T,V):
  for j in range(N):
    if T[j]==0:
      for i in range(N):
          if A[i][j]|A[i][j]==T[j]:
              continue
          else:
              return False
    elif T[j]==1:
       for i in range(N):
          if A[i][j]&A[i][j]==T[i]:
             continue
          else:
             return False

def try_A(i,j):
    if i==N and j==N:
      return A
    else:
      for x in range(max_uv):
        A[i][j]=x
        if row_bit(A,S,U):
            try_A(i+1,j)
        if col_bit(A,T,V):
            try_A(i,j+1)
        A[i][j]=0

print(try_A(0,0))

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(65)]
    three = []
    two = []
    x = y = 0
    for k in range(65):
        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()