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: Runtime Error

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

    to_bin = lambda x:str(bin(int(x))[2:])
    process_length = lambda x:'0' * (max_n - len(x)) + x
    N = int(sysread())
    S = list(map(int, sysread().split()))
    T = list(map(int, sysread().split()))
    U = list(map(to_bin, sysread().split()))
    V = list(map(to_bin, sysread().split()))

    max_n = len(str(max(U+V)))

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

    Z = (np.zeros((N,N,max_n), dtype=np.int32) - 1).astype(str)

    # fill absolutely determined columns
    for i, (s, val) in enumerate(zip(S,U)):
        if s == 0:# all 1
            is_1 = [k for k in range(len(val)) if val[k]=='1']
            for k in is_1:
                for j in range(N):
                    Z[i, j, k] = '1'
        else:# all 0
            is_0 = [k for k in range(len(val)) if val[k]=='0']
            for k in is_0:
                for j in range(N):
                    if Z[i, j, k] == '1':
                        print(-1)
                        return
                    Z[i, j, k] = '0'

    for j, (t, val) in enumerate(zip(T,V)):
        if t == 0:# all 1
            is_1 = [k for k in range(len(val)) if val[k]=='1']

            for k in is_1:
                for i in range(N):
                    if Z[i, j, k] == '0':
                        print(-1)
                    Z[i, j, k] = '1'
        else:# all 0
            is_0 = [k for k in range(len(val)) if val[k]=='0']
            for k in is_0:
                for i in range(N):
                    if Z[i, j, k] == '1':
                        print(-1)
                        return
                    Z[i, j, k] = '0'

    # fill empyty 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 '1' in Z[i, :, idx]:continue
                    is_empty = [k for k,_v in enumerate(Z[i, :, idx]) if _v=='-1']
                    if is_empty == []:
                        return print(-1)
                    for e in is_empty:
                        if T[e] == 0:
                            if (Z[:, e, idx] == '0').sum() > 0:
                                Z[i, e, idx] = '1'
                                break
                        else:
                            Z[i, e, idx] = '1'
                            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 '1' in Z[:, j, idx]: continue
                    is_empty = [k for k,_v in enumerate(Z[:, j, idx]) if _v == '-1']
                    if is_empty == []:
                        return print(-1)
                    for e in is_empty:
                        if S[e] == 0:
                            if (Z[e, :, idx] == '0').sum() > 0:
                                Z[e, j, idx] = '1'
                                break
                        else:
                            Z[e, j, idx] = '1'
                            break

    for i in range(N):
        for j in range(N):
            for m in range(max_n):
                if Z[i,j,m] == '-1':
                    Z[i,j,m] = '0'
    A = [[0] * N for _ in range(N)]

    for i in range(N):
        for j in range(N):
            tmp = str(int(''.join(Z[i,j,:].tolist()), 2))
            A[i][j] = tmp


    for s in A:
        print(' '.join(s))

if __name__ == "__main__":
    run()

Submission status: Runtime Error

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

    to_bin = lambda x:str(bin(int(x))[2:])
    process_length = lambda x:'0' * (max_n - len(x)) + x
    N = int(sysread())
    S = list(map(int, sysread().split()))
    T = list(map(int, sysread().split()))
    U = list(map(to_bin, sysread().split()))
    V = list(map(to_bin, sysread().split()))

    max_n = len(str(max(U+V)))

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

    Z = np.zeros((N,N,max_n)).astype(np.int32) - 1

    # fill absolutely determined columns
    for i, (s, val) in enumerate(zip(S,U)):
        if s == 0:# all 1
            is_1 = [k for k in range(len(val)) if val[k]=='1']
            for k in is_1:
                for j in range(N):
                    Z[i, j, k] = 1
        else:# all 0
            is_0 = [k for k in range(len(val)) if val[k]=='0']
            for k in is_0:
                for j in range(N):
                    if Z[i, j, k] == 1:
                        print(-1)
                        return
                    Z[i, j, k] = 0

    for j, (t, val) in enumerate(zip(T,V)):
        if t == 0:# all 1
            is_1 = [k for k in range(len(val)) if val[k]=='1']

            for k in is_1:
                for i in range(N):
                    if Z[i, j, k] == 0:
                        print(-1)
                    Z[i, j, k] = 1
        else:# all 0
            is_0 = [k for k in range(len(val)) if val[k]=='0']
            for k in is_0:
                for i in range(N):
                    if Z[i, j, k] == 1:
                        print(-1)
                        return
                    Z[i, j, k] = 0

    # fill empyty 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 1 in Z[i, :, idx]:continue
                    is_empty = [k for k,_v in enumerate(Z[i, :, idx]) if _v==-1]
                    if is_empty == []:
                        return print(-1)
                    for e in is_empty:
                        if T[e] == 0:
                            if (Z[:, e, idx] == 0).sum() > 0:
                                Z[i, e, idx] = 1
                                break
                        else:
                            Z[i, e, idx] = 1
                            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 1 in Z[:, j, idx]: continue
                    is_empty = [k for k,_v in enumerate(Z[:, j, idx]) if _v == -1]
                    if is_empty == []:
                        return print(-1)
                    for e in is_empty:
                        if S[e] == 0:
                            if (Z[e, :, idx] == 0).sum() > 0:
                                Z[e, j, idx] = 1
                                break
                        else:
                            Z[e, j, idx] = 1
                            break

    for i in range(N):
        for j in range(N):
            for m in range(max_n):
                if Z[i,j,m] == -1:
                    Z[i,j,m] = 0
    A = [[0] * N for _ in range(N)]

    Z = Z.astype(str)
    for i in range(N):
        for j in range(N):
            tmp = str(int(''.join(Z[i,j,:].tolist()), 2))
            A[i][j] = tmp


    for s in A:
        print(' '.join(s))

if __name__ == "__main__":
    run()

Submission status: Runtime Error

Solution code:
n=int(input())
s=list(map(int,input().split()))
t=list(map(int,input().split()))
u=list(map(int,input().split()))
v=list(map(int,input().split()))
ans=[n*[0]for _ in range(n)]
def f(u,v):
  q=[[(u[i]&v[j])|(0if s[i]else u[i])|(0if t[j]else v[j])for j in range(n)]for i in range(n)]
  if 0==sum(s[i]*u[i]for i in range(n))*sum(t[j]*v[j]for j in range(n)):
    if sum(s[i]*u[i]for i in range(n)):
      j=0
      for i in range(n):
        if s[i]*u[i]==0:continue
        while t[j]:j+=1
        q[i][j]=u[i]
        j+=1
    else:
      i=0
      for j in range(n):
        if t[j]*v[j]==0:continue
        while s[i]:i+=1
        q[i][j]=v[j]
        i+=1
  h=q[0][:]
  w=[i[0]for i in q]
  for i in range(n):
    for j in range(n):
      if s[i]:w[i]|=q[i][j]
      else:w[i]&=q[i][j]
      if t[j]:h[j]|=q[i][j]
      else:h[j]&=q[i][j]
  if(v!=h)|(u!=w):print(-1);exit() 
  return q
for k in range(64):
  r=f([a&1<<k for a in u],[a&1<<k for a in v])
  ans=[[r[i][j]|ans[i][j]for j in range(n)]for i in range(n)]
for i in ans:print(*i)

Submission status: Runtime Error

Solution code:
import os

import sys
import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N = int(sys.stdin.buffer.readline())
row_or = list(map(int, sys.stdin.buffer.readline().split()))
col_or = list(map(int, sys.stdin.buffer.readline().split()))
right = list(map(int, sys.stdin.buffer.readline().split()))
down = list(map(int, sys.stdin.buffer.readline().split()))

row_or = np.array(row_or, dtype=bool)
row_and = ~row_or
col_or = np.array(col_or, dtype=bool)
col_and = ~col_or
right = np.array(right, dtype=int)
down = np.array(down, dtype=int)


def solve(right, down):
    ret = np.full((N, N), -1, dtype=int)
    yet_rows = []
    yet_cols = []
    used = [False] * 2
    for r in range(N):
        if row_or[r] and right[r] == 0:
            ret[r, :] = 0
            used[0] = True
        elif row_and[r] and right[r] == 1:
            ret[r, :] = 1
            used[1] = True
        else:
            yet_rows.append(r)
    for c in range(N):
        if col_or[c] and down[c] == 0:
            ret[:, c] = 0
            used[0] = True
        elif col_and[c] and down[c] == 1:
            ret[:, c] = 1
            used[1] = True
        else:
            yet_cols.append(c)

    if len(yet_cols) == 1:
        c = yet_cols[0]
        if not used[0]:
            # 0 を優先的に使いたい
            ret[yet_rows, c] = 0
            for r in yet_rows:
                # and で 0 のところは 0 じゃないといけないので、
                # それ以外のところを 1 つ 1 にする
                if row_or[r] and right[r] == 1:
                    ret[r, c] = 1
                    break
        elif not used[1]:
            # 1 を優先的に使いたい
            ret[yet_rows, c] = 1
            for r in yet_rows:
                # or で 1 のところは 1 じゃないといけないので、
                # それ以外のところを 1 つ 0 にする
                if row_and[r] and right[r] == 0:
                    ret[r, c] = 0
                    break
        else:
            # どっちでもいい
            ret[yet_rows, c] = np.arange(len(yet_rows), dtype=int) % 2
    elif len(yet_rows) == 1:
        r = yet_rows[0]
        if not used[0]:
            # 0 を優先的に使いたい
            ret[r, yet_cols] = 0
            for c in yet_cols:
                # and で 0 のところは 0 じゃないといけないので、
                # それ以外のところを 1 つ 1 にする
                if col_or[c] and down[c] == 1:
                    ret[r, c] = 1
                    break
        elif not used[1]:
            # 1 を優先的に使いたい
            ret[r, yet_cols] = 1
            for c in yet_cols:
                # or で 1 のところは 1 じゃないといけないので、
                # それ以外のところを 1 つ 0 にする
                if col_and[c] and down[c] == 0:
                    ret[r, c] = 0
                    break
        else:
            # どっちでもいい
            ret[r, yet_cols] = np.arange(len(yet_cols), dtype=int) % 2
    else:
        # 行列両方 2 つ以上ある
        for i, r in enumerate(yet_rows):
            ret[r, yet_cols] = (np.arange(len(yet_cols), dtype=int) + i) % 2

    # つくった
    ok = True
    for r in range(N):
        if row_and[r]:
            if right[r] == 1:
                ok &= np.all(ret[r, :] == 1)
            else:
                ok &= np.any(ret[r, :] == 0)
        else:
            if right[r] == 1:
                ok &= np.any(ret[r, :] == 1)
            else:
                ok &= np.all(ret[r, :] == 0)
    for c in range(N):
        if col_and[c]:
            if down[c] == 1:
                ok &= np.all(ret[:, c] == 1)
            else:
                ok &= np.any(ret[:, c] == 0)
        else:
            if down[c] == 1:
                ok &= np.any(ret[:, c] == 1)
            else:
                ok &= np.all(ret[:, c] == 0)
    return ret, ok


ans = np.zeros((N, N), dtype=int)
for i in range(64):
    bits, ok = solve(right >> i & 1, down >> i & 1)
    if not ok:
        print(-1)
        exit()
    ans |= bits << i

for row in ans:
    print(*row)

Submission status: Runtime Error

Solution code:
import os

import sys
import numpy as np

from libs.debug import debug

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N = int(sys.stdin.buffer.readline())
row_or = list(map(int, sys.stdin.buffer.readline().split()))
col_or = list(map(int, sys.stdin.buffer.readline().split()))
right = list(map(int, sys.stdin.buffer.readline().split()))
down = list(map(int, sys.stdin.buffer.readline().split()))

row_or = np.array(row_or, dtype=bool)
row_and = ~row_or
col_or = np.array(col_or, dtype=bool)
col_and = ~col_or
right = np.array(right, dtype=int)
down = np.array(down, dtype=int)


@debug
def solve(right, down):
    ret = np.full((N, N), -1, dtype=int)
    yet_rows = []
    yet_cols = []
    used = [False] * 2
    for r in range(N):
        if row_or[r] and right[r] == 0:
            ret[r, :] = 0
            used[0] = True
        elif row_and[r] and right[r] == 1:
            ret[r, :] = 1
            used[1] = True
        else:
            yet_rows.append(r)
    for c in range(N):
        if col_or[c] and down[c] == 0:
            ret[:, c] = 0
            used[0] = True
        elif col_and[c] and down[c] == 1:
            ret[:, c] = 1
            used[1] = True
        else:
            yet_cols.append(c)

    if len(yet_cols) == 1:
        c = yet_cols[0]
        if not used[0]:
            # 0 を優先的に使いたい
            ret[yet_rows, c] = 0
            for r in yet_rows:
                # and で 0 のところは 0 じゃないといけないので、
                # それ以外のところを 1 つ 1 にする
                if row_or[r] and right[r] == 1:
                    ret[r, c] = 1
                    break
        elif not used[1]:
            # 1 を優先的に使いたい
            ret[yet_rows, c] = 1
            for r in yet_rows:
                # or で 1 のところは 1 じゃないといけないので、
                # それ以外のところを 1 つ 0 にする
                if row_and[r] and right[r] == 0:
                    ret[r, c] = 0
                    break
        else:
            # どっちでもいい
            ret[yet_rows, c] = np.arange(len(yet_rows), dtype=int) % 2
    elif len(yet_rows) == 1:
        r = yet_rows[0]
        if not used[0]:
            # 0 を優先的に使いたい
            ret[r, yet_cols] = 0
            for c in yet_cols:
                # and で 0 のところは 0 じゃないといけないので、
                # それ以外のところを 1 つ 1 にする
                if col_or[c] and down[c] == 1:
                    ret[r, c] = 1
                    break
        elif not used[1]:
            # 1 を優先的に使いたい
            ret[r, yet_cols] = 1
            for c in yet_cols:
                # or で 1 のところは 1 じゃないといけないので、
                # それ以外のところを 1 つ 0 にする
                if col_and[c] and down[c] == 0:
                    ret[r, c] = 0
                    break
        else:
            # どっちでもいい
            ret[r, yet_cols] = np.arange(len(yet_cols), dtype=int) % 2
    else:
        # 行列両方 2 つ以上ある
        for i, r in enumerate(yet_rows):
            ret[r, yet_cols] = (np.arange(len(yet_cols), dtype=int) + i) % 2

    # つくった
    ok = True
    for r in range(N):
        if row_and[r]:
            if right[r] == 1:
                ok &= np.all(ret[r, :] == 1)
            else:
                ok &= np.any(ret[r, :] == 0)
        else:
            if right[r] == 1:
                ok &= np.any(ret[r, :] == 1)
            else:
                ok &= np.all(ret[r, :] == 0)
    for c in range(N):
        if col_and[c]:
            if down[c] == 1:
                ok &= np.all(ret[:, c] == 1)
            else:
                ok &= np.any(ret[:, c] == 0)
        else:
            if down[c] == 1:
                ok &= np.any(ret[:, c] == 1)
            else:
                ok &= np.all(ret[:, c] == 0)
    return ret, ok


ans = np.zeros((N, N), dtype=int)
for i in range(64):
    bits, ok = solve(right >> i & 1, down >> i & 1)
    if not ok:
        print(-1)
        exit()
    ans |= bits << i

for row in ans:
    print(*row)

Submission status: Runtime Error

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:
            raise
            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: Runtime Error

Solution code:
import numpy as np


def solve(n, sss, ttt, uuu, vvv):
    max_d = max(max(uuu).bit_length(), max(vvv).bit_length())
    ans = np.zeros((n, n), dtype=np.uint64)

    for d in range(max_d):
        k = 1 << d
        ambiguous_i = []
        ambiguous_j = []
        filled_i = [False, False]
        filled_j = [False, False]
        for i, (s, u) in enumerate(zip(sss, uuu)):
            if s == 0:
                if u & k:
                    ans[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
                    ans[:, 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:
                    ans[i, :] += k
                else:
                    pass
            else:
                ok = False
                for j, bj in ambiguous_j:
                    ans[i, j] += bj * 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:
                    ans[:, j] += k
                else:
                    pass
            else:
                ok = False
                for i, bi in ambiguous_i:
                    ans[i, j] += bi * 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

    ans = ans.tolist()
    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)
for row in ans:
    print(*row)

Submission status: Runtime Error

Solution code:
n = int(input())
assert n == 2

Submission status: Runtime Error

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 i in range(N):
        for j in range(N):
            if S[i] == T[j]:
                ans[i] += ' ' + str(U[i] & V[j])
            else:
                ans[i] += ' ' + str(U[i] | V[j])
    print('\n'.join(ans))
    return

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def transpose(mat, tr, N): 
    for i in range(N): 
        for j in range(N): 
            tr[i][j] = mat[j][i] 
def isSymmetric(mat, N):
    tr = [ [0 for j in range(len(mat[0])) ] for i in range(len(mat)) ] 
    transpose(mat, tr, N) 
    for i in range(N): 
        for j in range(N): 
            if (mat[i][j] != tr[i][j]): 
                return False
    return True
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()))
l1=[]
l1.append(s)
l1.append(t)
l2=[]
l2.append(u)
l2.append(v)
if n==1:
    if u[0]==v[0]:
        print(u[0])
    else:
        print(-1)
elif isSymmetric(l1,n) and isSymmetric(l2,n):
    ans=[[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]==1:
                ans[i][j]=u[i]
            elif s[i]==1 and t[j]==0:
                ans[i][j]=v[j]
            else:
                ans[i][j]=u[i]
    for i in range(n):
        for j in range(n):
            print(ans[i][j],end=" ")
        print()
else:
    print(-1)

Submission status: Runtime Error

Solution code:
n=int(input())
s_li=list(map(int,input().split()))
t_li=list(map(int,input().split()))
u_li=list(map(int,input().split()))
v_li=list(map(int,input().split()))

kano=True

ans=[[0]*n for i in range(n)]

hidari=[0]*n

migi=[0]*n

for i in range(n):
    if(n==1):
        kano=False
        break
    s=s_li[i]
    t=t_li[i]
    u=u_li[i]
    v=v_li[i]
    if(s==t):
        if(u==v):
            hidari[i]=u
            migi[i]=u
        else:
            kano=False
            break
    else:
        wa=0
        seki=0
        if(s==1):
            wa=u
            seki=v
        else:
            wa=v
            seki=u
        if((wa&seki)==seki):
            hidari[i]=wa
            migi[i]=seki
        else:
            kano=False
            break

#print(hidari)
#print(migi)

for i in range(n):
    ans[i][0]=hidari[i]
    for j in range(1,n):
        ans[i][j]=migi[i]

if(kano):
    for i in range(n):
        #print(ans[i])
        moji=""
        for j in range(n):
            moji+=str(ans[i][j])+" "
        print(moji)
else:
    if(n!=1):
        re=1/0
        print(-1)
    else:
        if(u_li[0]==v_li[0]):
            print(u_li[0])
        else:
            print(-1)

Submission status: Runtime Error

Solution code:
def transpose(mat, tr, N): 
    for i in range(N): 
        for j in range(N): 
            tr[i][j] = mat[j][i] 
def isSymmetric(mat, N):
    tr = [ [0 for j in range(len(mat[0])) ] for i in range(len(mat)) ] 
    transpose(mat, tr, N) 
    for i in range(N): 
        for j in range(N): 
            if (mat[i][j] != tr[i][j]): 
                return False
    return True
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()))
l1=[]
l1.append(s)
l1.append(t)
l2=[]
l2.append(u)
l2.append(v)
if isSymmetric(l1,n) and isSymmetric(l2,n):
    ans=[[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]==1:
                ans[i][j]=u[i]
            elif s[i]==1 and t[j]==0:
                ans[i][j]=v[j]
            else:
                ans[i][j]=u[i]
    for i in range(n):
        for j in range(n):
            print(ans[i][j],end=" ")
        print()
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import heapq


def inpl():
    return list(map(int, input().split()))


N, M, S = inpl()
A = 50  # max A
S = min(S, M * A)

lines = [[] for _ in range(N * (M * A + 1))]
for _ in range(M):
    in_s, in_t, cost, time = inpl()
    in_s -= 1
    in_t -= 1

    for n in range(cost, M * A + 1):
        s = in_s + n * N
        t = in_t + (n - cost) * N
        lines[s].append((t, time))

        s = in_s + (n - cost) * N
        t = in_t + n * N
        lines[t].append((s, time))


for i in range(N):
    cost, time = inpl()
    for n in range(0, M * A + 1):
        s = i + n * N
        if n + cost > M * A:
            t = i + (M * A) * N
        else:
            t = i + (n + cost) * N
        lines[s].append((t, time))


INF = 10**20


def dijkstra(N, S):
    weight = [INF] * N
    weight[S] = 0
    q = [[0, S]]
    heapq.heapify(q)
    while q:
        w0, s = heapq.heappop(q)
        for t, w in lines[s]:
            w += w0
            if weight[t] > w:
                heapq.heappush(q, [w, t])
                weight[t] = w

    return weight


weight = dijkstra(N * (M * A + 1), 0 + S * N)

print("\n".join(map(str, weight[1:N])))

Submission status: Runtime Error

Solution code:
n=int(input())
s_li=list(map(int,input().split()))
t_li=list(map(int,input().split()))
u_li=list(map(int,input().split()))
v_li=list(map(int,input().split()))

kano=True

ans=[[0]*n for i in range(n)]

hidari=[0]*n

migi=[0]*n

for i in range(n):
    if(n==1):
        kano=False
        break
    s=s_li[i]
    t=t_li[i]
    u=u_li[i]
    v=v_li[i]
    if(s==t):
        if(u==v):
            hidari[i]=u
            migi[i]=u
        else:
            kano=False
            break
    else:
        wa=0
        seki=0
        if(s==1):
            wa=u
            seki=v
        else:
            wa=v
            seki=u
        if((wa&seki)==seki):
            hidari[i]=wa
            migi[i]=seki
        else:
            kano=False
            break

#print(hidari)
#print(migi)

for i in range(n):
    ans[i][0]=hidari[i]
    for j in range(1,n):
        ans[i][j]=migi[i]

if(kano):
    for i in range(n):
        #print(ans[i])
        moji=""
        for j in range(n):
            moji+=str(ans[i][j])+" "
        print(moji)
else:
    if(n!=1):
        print(-1)
    else:
        if(u[0]==v[0]):
            print(u[0])
        else:
            print(-1)

Submission status: Runtime Error

Solution code:
A, B, C, D = map(int, input().split())
counter = 1
while A > 0 and C > 0:
    if counter % 2 == 1:
        '''T 攻击'''
        C -= B
    elif counter % 2 == 0:
        A -= D
    counter += 1
if A <= 0:
    print('No')
else:
    print('Yes')