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

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def ok():
        for s,u,row in zip(ss,uu,ans):
            b=row[0]
            if s:
                for a in row[1:]:b|=a
                if b!=u:return False
            else:
                for a in row[1:]:b&=a
                if b!=u:return False
        for t,v,col in zip(tt,vv,zip(*ans)):
            b=col[0]
            if t:
                for a in col[1:]:b|=a
                if b!=v:return False
            else:
                for a in col[1:]:b&=a
                if b!=v:return False
        return True

    mxk=6
    n=II()
    ss=LI()
    tt=LI()
    uu=LI()
    vv=LI()
    # 2進数の各桁ごとに考える
    # bit[k][i][j]...答えのi行j列目のkけた目が入る
    bit=[[[-1]*n for _ in range(n)] for _ in range(mxk)]
    # ORが0⇒その行（または列）はすべて0
    # ANDが1⇒その行（または列）はすべて1
    # と、確定するので、そのマスを埋める
    for i,(s,u) in enumerate(zip(ss,uu)):
        for k in range(mxk):
            if u>>k&1==1-s:
                for j in range(n):bit[k][i][j]=1-s
    for j,(t,v) in enumerate(zip(tt,vv)):
        for k in range(mxk):
            if v>>k&1==1-t:
                for i in range(n):bit[k][i][j]=1-t
    # 確定しなかった（値が初期値-1の）マスについて決めていく
    # ORが1ならば、少なくとも1マスは1が必要
    # ANDが0ならば、少なくとも1マスは0が必要
    for k in range(mxk):
        # -1を含む行と列を調べる
        ii=[]
        jj=[]
        for i,row in enumerate(bit[k]):
            if -1 in row:ii.append(i)
        for j,col in enumerate(zip(*bit[k])):
            if -1 in col:jj.append(j)
        # -1がなければ何もしない
        if not ii:continue
        # 行が1つしかない場合
        # 行の条件を満たすデフォルト値dfを決めておく
        # それぞれの列の条件をみながら、すでに条件を満たしていればデフォルト値を
        # 満たしていなければ、満たすような値を-1と置き換えていく
        if len(ii)==1:
            df=ss[ii[0]]
            for j in jj:
                for i in range(n):
                    if bit[k][i][j]==tt[j]:
                        bit[k][ii[0]][j]=df
                        break
                else:
                    bit[k][ii[0]][j] = tt[j]
        # 列が1つしかない場合
        # 行のときと同じやり方
        elif len(jj) == 1:
            df = tt[jj[0]]
            for i in ii:
                for j in range(n):
                    if bit[k][i][j] == ss[i]:
                        bit[k][i][jj[0]] = df
                        break
                else:
                    bit[k][i][jj[0]] = ss[i]
        # 行も列も2つ以上あるとき
        # すべての行と列に0と1が入るようにすればいい
        else:
            # まず、すべて0にする
            for i in ii:
                for j in jj:
                    bit[k][i][j] = 0
            # 行と列の要素数が違う場合、短い方に付け足して同じ長さにする
            # 付け足すのはどの要素でもいい（今回は先頭にした）
            if len(ii)<len(jj):ii+=[ii[0]]*(len(jj)-len(ii))
            if len(ii)>len(jj):jj+=[jj[0]]*(len(ii)-len(jj))
            # 行と列を同時にずらしながら1を埋める
            # 各行列に最低1つは1を入れていく感じ
            for i,j in zip(ii,jj):
                bit[k][i][j]=1
    #for k in range(mxk):
        #print("k=",k)
        #p2D(bit[k])
    # 各桁をみながら答えを作る
    ans=[[0]*n for _ in range(n)]
    for k in range(mxk):
        for i in range(n):
            for j in range(n):
                ans[i][j]+=bit[k][i][j]<<k
    # 条件を満たしているかの確認
    if ok():
        for row in ans:
            print(*row)
    else:print(-1)

main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def ok():
        for s,u,row in zip(ss,uu,ans):
            b=row[0]
            if s:
                for a in row[1:]:b|=a
                if b!=u:return False
            else:
                for a in row[1:]:b&=a
                if b!=u:return False
        for t,v,col in zip(tt,vv,zip(*ans)):
            b=col[0]
            if t:
                for a in col[1:]:b|=a
                if b!=v:return False
            else:
                for a in col[1:]:b&=a
                if b!=v:return False
        return True

    n=II()
    ss=LI()
    tt=LI()
    uu=LI()
    vv=LI()
    ans=[[0]*n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            if ss[i]==tt[j]:
                if ss[i]:ans[i][j]=uu[i]&vv[j]
                else:ans[i][j]=uu[i]|vv[j]
            else:
                if ss[i]:ans[i][j]=vv[j]
                else:ans[i][j]=uu[i]

    if ok():
        for row in ans:
            print(*row)
    else:print(-1)

main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def ok():
        for s,u,row in zip(ss,uu,ans):
            b=row[0]
            if s:
                for a in row[1:]:b|=a
                if b!=u:return False
            else:
                for a in row[1:]:b&=a
                if b!=u:return False
        for t,v,col in zip(tt,vv,zip(*ans)):
            b=col[0]
            if t:
                for a in col[1:]:b|=a
                if b!=v:return False
            else:
                for a in col[1:]:b&=a
                if b!=v:return False
        return True

    n=II()
    ss=LI()
    tt=LI()
    uu=LI()
    vv=LI()
    ans=[[0]*n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            if ss[i]|tt[j]:ans[i][j]=uu[i]&vv[j]
            else:ans[i][j]=uu[i]|vv[j]
    if ok():
        for row in ans:
            print(*row)
    else:print(-1)

main()

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(map(lambda x: bool(int(x)), input().split()))
T = list(map(lambda x: bool(int(x)), input().split()))
U = list(map(lambda x: '{:064b}'.format(int(x)), input().split()))
V = list(map(lambda x: '{:064b}'.format(int(x)), input().split()))

ans = [[['0'] * 64 for _ in range(N)] for _ in range(N)]

for b in range(64):
    for i in range(N):
        for j in range(N):
            is0_r = (S[i] and U[i][b] == '0')
            is1_r = ((not S[i]) and U[i][b] == '1')
            is0_c = (T[j] and V[j][b] == '0')
            is1_c = ((not T[j]) and V[j][b] == '1')
            if (is0_r and is1_c) or (is1_r and is0_c):
                print(-1)
                exit()
            if is0_r or is0_c:
                ans[i][j][b] = '0'
            elif is1_r or is1_c:
                ans[i][j][b] = '1'
            elif U[i][b] == V[j][b]:
                ans[i][j][b] = V[j][b]

for b in range(64):
    for i in range(N):
        if not (S[i] and U[i][b] == 1):
            continue
        ok = False
        for j in range(N):
            if ans[i][j][b] == '1' or ((not T[j]) and V[j][b] == '0'):
                ans[i][j][b] == '1'
                ok = True
        if not ok:
            print(-1)
            exit()
    for j in range(N):
        if not (T[j] and V[j][b] == 1):
            continue
        ok = False
        for i in range(N):
            if ans[i][j][b] == '1' or ((not S[i]) and U[i][b] == '0'):
                ans[i][j][b] == '1'
                ok = True
        if not ok:
            print(-1)
            exit()

for i in range(N):
    print(' '.join(map(lambda x: str(int(x, 2)),
                       [''.join(r) for r in ans[i]])))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache

import heapq


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N = inputI()
S = inputIL()
T = inputIL()
U = inputIL()
V = inputIL()


ans = [[0 for i in range(N)] for j in range(N)]
for n in range(64):
  m = [[0.0001 for i in range(N)] for j in range(N)]
  #行の前処理
  for i in range(N):
    s,u = S[i],(U[i]>>n)&1, 
    if s==0 and u==1:
      m[i] = [1 for i in range(N)]
    if s==1 and u==0:
      m[i] = [0 for i in range(N)]
  #列の前処理
  for j in range(N):
    t,v = T[j],(V[j]>>n)&1, 
    if t==0 and v==1:
      for i in range(N):
        if m[i][j] == 0:
          print(-1)
          exit()
        else:
          m[i][j] = 1
    if t==1 and v==0:
      for i in range(N):
        if m[i][j] == 1:
          print(-1)
          exit()
        else:
          m[i][j] = 0
  #確定マスの処理
  for i in range(N):
    for j in range(N):
      s,u = S[i],(U[i]>>n)&1
      t,v = T[j],(V[j]>>n)&1
      if (s,u) == (t,v):
        m[i][j] = u
  memo_gyo = [sum(m[i])//1 for i in range(N)]
  memo_retu = [0 for j in range(N)]
  for i in range(N):
    for j in range(N):
      memo_retu[j] += m[i][j]//1

  #行の確認
  for i in range(N):
    s,u = S[i],(U[i]>>n)&1
    if s==1 and u==1:
      if sum(m[i]) >= 1:
        pass
      else:
        for j in range(N):
          t,v = T[j],(V[j]>>n)&1
          if t==0 and v==0:
            if memo_retu[j] >= N-1:
              print(-1)
              exit()
            else:
              m[i][j] = 1
              memo_gyo[i] += 1
              memo_retu[j] += 1
  #列の確認
  for j in range(N):
    t,v = T[i],(V[i]>>n)&1
    if t==1 and v==1:
      if memo_retu[j] >= 1:
        pass
      else:
        for i in range(N):
          s,u = S[j],(U[j]>>n)&1
          if s==0 and u==0:
            if memo_gyo[i] >= N-1:
              print(-1)
              exit()
            else:
              m[i][j] = 1
              memo_gyo[i] += 1
              memo_retu[j] += 1
              
  for i in range(N):
    for j in range(N):
      ans[i][j] += int(m[i][j]//1)<<n
      
      
for item in ans:
  print(" ".join(map(str,item)))

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

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
        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):
                    ans[i][j] += u*n
                else:
                    possible = False
            elif (s,u)==(1,0):
                if (t,v)==(0,1):
                    possible = False
            elif (t,v)==(0,1) or (t,v)==(1,0):
                ans[i][j] += v*n
            elif (s,u)==(t,v):
                ans[i][j] += u*n
            elif (s,u)==(0,0):
                if n1==0:
                    i1 = i
                    j1 = j
                else:
                    if i==i1 or j==j1:
                        ans[i][j] += n
                n1 += 1
            elif (s,u)==(1,1):
                if n2==0:
                    i2 = i
                    j2 = j
                else:
                     if i==i2 or j==j2:
                         ans[i][j] += n
                n2 += 1
            if u*n==ans[i][j]&n:
                S_[i] = True
            if v*n==ans[i][j]&n:
                T_[j] = True
    if sum(S_)!=N or sum(T_)!=N:
        possible = False

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

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

ans = [[0]*N for _ in range(N)]

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

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

Submission status: Wrong Answer

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='uint64')
down = np.array(down, dtype='uint64')


def solve(right, down):
    ret = np.full((N, N), 7, dtype='uint64')
    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)
    assert np.all(ret <= 1)
    return ret & 1, ok


ans = np.zeros((N, N), dtype='uint64')
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: Wrong Answer

Solution code:
s = input()
n = len(s)
ans = 0
now = 0
mp = [0 for i in range(2019)]
mp[0] += 1
tmp = [0 for i in range(2019)]

id = [0 for i in range(2019)]
for i in range(2019):
    id[i] = i * 10 % 2019

for c in s:
    now *= 10;
    now += ord(c) - ord("0")
    now %= 2019

    ans += mp[now]
    mp[now] += 1
    for i in range(2019):
        tmp[id[i]] = mp[i]
    for i in range(2019):
        mp[i] = tmp[i]

print(ans)

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

mat = [[[4]*64 for _ in range(n)] for _ in range(n)]
flag=True
for i in range(n):
    for j in range(n):
        for k in range(64):
            a = (u[i] & 1 << k) >> k
            b = (v[j] & 1 << k) >> k
            if s[i] and t[j]:
                if a == 0 or b == 0:
                    mat[i][j][k]=0
                else:
                    mat[i][j][k]=1
            elif s[i] == 0 and t[j] == 1:
                if a == 1 and b == 0:
                    flag=False
                elif a == 1:
                    mat[i][j][k]=1
                elif b == 0:
                    mat[i][j][k]=0
                else:
                    mat[i][j][k]=2

            elif s[i] == 1 and t[j] == 0:
                if a == 0 and b == 1:
                    flag=False
                elif a == 0:
                        mat[i][j][k]=0
                elif b == 1:
                    mat[i][j][k]=1
                else:
                    mat[i][j][k]=2

            else:
                if a == 1 or b == 1:
                    mat[i][j][k]=1
                else:
                    mat[i][j][k]=0

exist1row =[[False]*64 for _ in range(n)]
exist1column = [[False]*64 for _ in range(n)]
exist0row = [[False]*64 for _ in range(n)]
exist0column = [[False]*64 for _ in range(n)]
for i in range(n):
    for j in range(n):
        for k in range(64):
            if mat[i][j][k]==1:
                exist1row[i][k] =True
                exist1column[j][k] =True
            if mat[i][j][k]==0:
                exist0row[i][k] =True
                exist0column[j][k] =True

for i in range(n):
    for j in range(n):
        for k in range(64):
            a = (u[i] & 1 << k) >> k
            b = (v[j] & 1 << k) >> k
            if s[i] == 0 and t[j] == 1:
                if a == 0 and b == 1:
                    if exist0row[i][k]:
                        mat[i][j][k]=1
                        exist1row[i][k] =True
                        exist1column[j][k] =True
                    else:
                        mat[i][j][k]=0
                        exist0row[i][k] =True
                        exist0column[j][k] =True                        

            elif s[i] == 1 and t[j] == 0:
                if a == 1 and b == 0:
                    if exist1row[i][k]:
                        mat[i][j][k]=0
                        exist0row[i][k] =True
                        exist0column[j][k] =True
                    else:
                        mat[i][j][k]=1
                        exist1row[i][k] =True
                        exist1column[j][k] =True


if flag:
    ans = [[0]*n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            for k in range(64):
                ans[i][j]+=mat[i][j][k]<<k

    for i in range(n):
        print(*ans[i])
else:
    print(-1)

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

mat = [[[4]*64 for _ in range(n)] for _ in range(n)]
flag=True
for i in range(n):
    for j in range(n):
        for k in range(64):
            a = (u[i] & 1 << k) >> k
            b = (v[j] & 1 << k) >> k
            if s[i] and t[j]:
                if a == 0 or b == 0:
                    mat[i][j][k]=0
                else:
                    mat[i][j][k]=1
            elif s[i] == 0 and t[j] == 1:
                if a == 1 and b == 0:
                    flag=False
                elif a == 1:
                    mat[i][j][k]=1
                elif b == 0:
                    mat[i][j][k]=0
                else:
                    mat[i][j][k]=2

            elif s[i] == 1 and t[j] == 0:
                if a == 0 and b == 1:
                    flag=False
                elif a == 0:
                        mat[i][j][k]=0
                elif b == 1:
                    mat[i][j][k]=1
                else:
                    mat[i][j][k]=2

            else:
                if a == 1 or b == 1:
                    mat[i][j][k]=1
                else:
                    mat[i][j][k]=0

exist1row = [[[False]*64 for _ in range(n)] for _ in range(n)]
exist1column = [[[False]*64 for _ in range(n)] for _ in range(n)]
exist0row = [[[False]*64 for _ in range(n)] for _ in range(n)]
exist0column = [[[False]*64 for _ in range(n)] for _ in range(n)]
for i in range(n):
    for j in range(n):
        for k in range(64):
            if mat[i][j][k]==1:
                for j2 in range(n):
                    exist1row[i][j2][k] =True
                for i2 in range(n):
                    exist1column[i2][j][k] =True
            if mat[i][j][k]==0:
                for j2 in range(n):
                    exist0row[i][j2][k] =True
                for i2 in range(n):
                    exist0column[i2][j][k] =True

for i in range(n):
    for j in range(n):
        for k in range(64):
            a = (u[i] & 1 << k) >> k
            b = (v[j] & 1 << k) >> k
            if s[i] == 0 and t[j] == 1:
                if a == 0 and b == 1:
                    if exist0row[i][j][k]:
                        mat[i][j][k]=1
                        for j2 in range(n):
                            exist1row[i][j2][k] =True
                        for i2 in range(n):
                            exist1column[i2][j][k] =True
                    else:
                        mat[i][j][k]=0
                        for j2 in range(n):
                            exist0row[i][j2][k] =True
                        for i2 in range(n):
                            exist0column[i2][j][k] =True                        

            elif s[i] == 1 and t[j] == 0:
                if a == 1 and b == 0:
                    if exist1row[i][j][k]:
                        mat[i][j][k]=0
                        for j2 in range(n):
                            exist0row[i][j2][k] =True
                        for i2 in range(n):
                            exist0column[i2][j][k] =True
                    else:
                        mat[i][j][k]=1
                        for j2 in range(n):
                            exist1row[i][j2][k] =True
                        for i2 in range(n):
                            exist1column[i2][j][k] =True


if flag:
    ans = [[0]*n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            for k in range(64):
                ans[i][j]+=mat[i][j][k]<<k

    for i in range(n):
        print(*ans[i])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:


n = 0
s = []
t = []
u = []
v = []

def format_input(filename = None):
	global n
	global s, t, u, v
	if filename == None:
		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()))

	elif filename == '__random__':
		from random import randint as rng
		n = rng(1, 5 * 10**2)
		s = [rng(0, 1) for i in range(n)]
		t = [rng(0, 1) for i in range(n)]
		u = [rng(0, 1) for i in range(n)]
		v = [rng(0, 1) for i in range(n)]
		print(n)
		print(' '.join(list(map(str, s))))
		print(' '.join(list(map(str, t))))
		print(' '.join(list(map(str, u))))
		print(' '.join(list(map(str, v))))

def get_answer():
	exist = 1
	a = [[0] * n for i in range(n)]
	for i in range(n):
		for j in range(n):
			if s[i] == 0:
				if t[j] == 0:
					a[i][j] = u[i] | v[j]
				else:
					if u[i] & ~(v[j]) != 0:
						exist = -1
						break
					else:
						a[i][j] = u[i] | 0
			else:
				if t[j] == 0:
					if ~(u[i]) & v[j] != 0:
						exist = -1
						break
					else:
						a[i][j] = 0 | v[j]
				else:
					a[i][j] = u[i] & v[j]
		else:
			continue
		break

	if exist == -1:
		print(exist)
	else:
		[print(' '.join(list(map(str, a[i])))) for i in range(n)]
	return exist

if __name__ == '__main__':
	format_input()

	ans = get_answer()