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:
#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

N = int(input())

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

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

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

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

  
for item in res:
  print(*item)

Submission status: Wrong Answer

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

N = int(input())

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

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

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

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

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

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

  
for item in res:
  print(*item)

Submission status: Wrong Answer

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

N = int(input())

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

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

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

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

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

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

  
for item in res:
  print(*item)

Submission status: Wrong Answer

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

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

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

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

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

  
for item in res:
  print(*item)

Submission status: Wrong Answer

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

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

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

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

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

  
for item in res:
  print(*item)

Submission status: Wrong Answer

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

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

import sys
input = sys.stdin.readline

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


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

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

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

N = inputI()
S,T,U,V = inputILL(4)

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

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

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

  
for item in res:
  print(*item)

Submission status: Wrong Answer

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

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

import sys
input = sys.stdin.readline

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


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

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

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

N = inputI()
S,T,U,V = inputILL(4)

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

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

for n in range(64):
  u = [(i>>n)&1 for i in U]
  v = [(i>>n)&1 for i in V]
  b = 0
  check = 0
  ###行のAND=1とOR=0を埋める###
  for i in range(N):
    if S[i] != u[i]:
      if u[i] == 1:
        b |= mask_gyo[i]
      check |= mask_gyo[i]
  ###列のAND=1とOR=0を埋める###
  for i in range(N):
    if T[i] != v[i]:
      if v[i] == 1:
        if b & mask_retu[i] != check & mask_retu[i]:
          print(-1)
          exit()
        b |= mask_retu[i]
      if v[i] == 0:
        if (b & mask_retu[i]) ^ (check & mask_retu[i]) != (check & mask_retu[i]):
          print(-1)
          exit()
      check |= mask_retu[i]
  ###行と列の値が一致するところを埋める###  
  for j in range(N):
    for i in range(N):
      if u[j] == v[i]:
        digit = (1<<i)<<(N*j)
        b |= digit*u[i]
        check |= digit
  ###行のOR=1を満たしていく###  
  for i in range(N):
    if S[i] == u[i] == 1:
      if b & mask_gyo[i] == 0: #まだ満たされてない行を発見した場合
        flag = 0 #未解決
        for j in range(N):
          digit = (1<<i)<<(N*j)
          if T[j]==1 and v[j]== 0 and check & digit == 0 and count_bit(b & mask_retu[j])<N-1:
            b |= digit
            check |= digit
            flag = 1 #解決
            break
        if not flag:
          print(-1)
          exit()
  ###列のOR=1を満たしていく###  
  for i in range(N):
    if T[i] == v[i] == 1:
      if b & mask_retu[i] == 0: #まだ満たされてない列を発見した場合
        flag = 0 #未解決
        for j in range(N):
          digit = (1<<j)<<(N*i)
          if S[j]==1 and u[j]== 0 and check & digit == 0 and count_bit(b & mask_gyo[j])<N-1:
            b |= digit
            check |= digit
            flag = 1 #解決
            break         
        if not flag:
          print(-1)
          exit()    
  
  for j in range(N):
    for i in range(N):
      a = not (0 == b & ((1<<i)<<(N*j)))
      res[i][j] += a<<n
  
for item in res:
  print(*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(lambda x:'0'*65+bin(int(x))[2:],input().split()))
v=list(map(lambda x:'0'*65+bin(int(x))[2:],input().split()))
ans=[[0]*n for _ in range(n)]
for i in range(65): # 2^iの位で考える
    ansi=[[-1]*n for _ in range(n)] # -1で残ったものは0とみなす
    for r in range(n): # 和で0、積で1の行と列に値を入れる。
        if s[r]==1 and u[r][-i-1]=='0':
            for c in range(n):
                ansi[r][c]=0
        elif s[r]==0 and u[r][-i-1]=='1':
            for c in range(n):
                ansi[r][c]=1
    for c in range(n):
        if t[c]==1 and v[c][-i-1]=='0':
            for r in range(n):
                if ansi[r][c]==-1:
                    ansi[r][c]=0
                elif ansi[r][c]==1:
                    print(-1,'a',i)
                    exit(0)
        elif t[c]==0 and v[c][-i-1]=='1':
            for r in range(n):
                if ansi[r][c]==-1:
                    ansi[r][c]=1
                elif ansi[r][c]==0:
                    print(-1,'b',i)
                    exit(0)
    for r in range(n):
        for c in range(n):
            if ansi[r][c]==-1 and u[r][-i-1]==v[c][-i-1]:
                ansi[r][c]=int(v[c][-i-1])


    for r in range(n): #行に対する操作
        if s[r]==1 and u[r][-i-1]=='1' and 1>max(ansi[r]): #和で1の行だが、まだ1がない
            ok=False
            for c in range(n):
                if ansi[r][c]<1 and t[c]==0 and v[c][-1-i]=='0' and 1>min([ansi[l][c] for l in range(n) if l!=r]): #積で0の列の0を1にする。ただし他の要素に0がある場合のみ。
                    ansi[r][c]=1
                    ok=True
                    break
            if ok==False:
                print(-1,'c',i)
                exit(0)
        if s[r]==0 and u[r][-i-1]=='0' and 0<min(ansi[r]): #積で0の行だが、まだ0がない
            ok=False
            for c in range(n):
                if ansi[r][c]==1 and t[c]==1 and v[c][-1-i]=='1' and 0<max([ansi[l][c] for l in range(n) if l!=r]): #和で1の列の1を0にする。ただし他の要素に1がある場合のみ。
                    ansi[r][c]=0
                    ok=True
                    break
            if ok==False:
                print(-1,'d',i)
                exit(0)
    for c in range(n): #列に対する操作
        if t[c]==1 and v[c][-i-1]=='1' and 1>max([ansi[l][c] for l in range(n)]): #和で1の列だが、まだ1がない
            ok=False
            for r in range(n):
                if ansi[r][c]<1 and s[r]==0 and u[r][-1-i]=='0' and 1>min([ansi[r][l] for l in range(n) if l!=c]): #積で0の行の0を1にする。ただし他の要素に0がある場合のみ。
                    ansi[r][c]=1
                    ok=True
                    break
            if ok==False:
                print(-1,'e',i)
                exit(0)
        if t[c]==0 and v[c][-i-1]=='0' and 0<min([ansi[l][c] for l in range(n)]): #積で0の列だが、まだ0がない
            ok=False
            for r in range(n):
                if ansi[r][c]==1 and s[c]==1 and u[c][-1-i]=='1' and 0<max([ansi[r][l] for l in range(n) if l!=c]): #和で1の行の1を0にする。ただし他の要素に1がある場合のみ。
                    ansi[r][c]=1
                    ok=True
                    break
            if ok==False:
                print(-1,'f',i)
                exit(0)
    for r in range(n):
        for c in range(n):
            ans[r][c]+=pow(2,i)*max(0,ansi[r][c])
for a in ans:
    print(' '.join(map(str,a)))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(map(int,input().split()))
t=list(map(int,input().split()))
u=list(map(lambda x:'0'*64+bin(int(x))[2:],input().split()))
v=list(map(lambda x:'0'*64+bin(int(x))[2:],input().split()))
ans=[[0]*n for _ in range(n)]
for i in range(65): # 2^iの位で考える
    ansi=[[-1]*n for _ in range(n)] # -1で残ったものは0とみなす
    for r in range(n): # 和で0、積で1の行と列に値を入れる。
        if s[r]==1 and u[r][-i-1]=='0':
            for c in range(n):
                ansi[r][c]=0
        elif s[r]==0 and u[r][-i-1]=='1':
            for c in range(n):
                ansi[r][c]=1
    for c in range(n):
        if t[c]==1 and v[c][-i-1]=='0':
            for r in range(n):
                if ansi[r][c]==-1:
                    ansi[r][c]=0
                elif ansi[r][c]==1:
                    print(-1)
                    exit(0)
        elif t[c]==0 and v[c][-i-1]=='1':
            for r in range(n):
                if ansi[r][c]==-1:
                    ansi[r][c]=1
                elif ansi[r][c]==0:
                    print(-1)
                    exit(0)
    for r in range(n):
        for c in range(n):
            if ansi[r][c]==-1 and u[r][-i-1]==v[c][-i-1]:
                ansi[r][c]=int(v[c][-i-1])

    for r in range(n): #行に対する操作
        if s[r]==1 and u[r][-i-1]=='1' and 1>max(ansi[r]): #和で1の行だが、まだ1がない
            ok=False
            for c in range(n):
                if ansi[r][c]<1 and t[c]==0 and u[c][-1-i]=='0' and 1>min([ansi[l][c] for l in range(n) if l!=r]): #積で0の列の0を1にする。ただし他の要素に0がある場合のみ。
                    ansi[r][c]=1
                    ok=True
                    break
            if ok==False:
                print(-1)
                exit(0)
        if s[r]==0 and u[r][-i-1]=='0' and 0<min(ansi[r]): #積で0の行だが、まだ0がない
            ok=False
            for c in range(n):
                if ansi[r][c]==1 and t[c]==1 and u[c][-1-i]=='1' and 0<max([ansi[l][c] for l in range(n) if l!=r]): #和で1の列の1を0にする。ただし他の要素に1がある場合のみ。
                    ansi[r][c]=0
                    ok=True
                    break
            if ok==False:
                print(-1)
                exit(0)
    for c in range(n): #列に対する操作
        if t[c]==1 and v[c][-i-1]=='1' and 1>max([ansi[l][c] for l in range(n)]): #和で1の列だが、まだ1がない
            ok=False
            for r in range(n):
                if ansi[r][c]<1 and s[r]==0 and v[r][-1-i]=='0' and 1>min([ansi[r][l] for l in range(n) if l!=c]): #積で0の行の0を1にする。ただし他の要素に0がある場合のみ。
                    ansi[r][c]=1
                    ok=True
                    break
            if ok==False:
                print(-1)
                exit(0)
        if t[c]==0 and v[c][-i-1]=='0' and 0<min([ansi[l][c] for l in range(n)]): #積で0の列だが、まだ0がない
            ok=False
            for r in range(n):
                if ansi[r][c]==1 and s[c]==1 and v[c][-1-i]=='1' and 0<max([ansi[r][l] for l in range(n) if l!=c]): #和で1の行の1を0にする。ただし他の要素に1がある場合のみ。
                    ansi[r][c]=1
                    ok=True
                    break
            if ok==False:
                print(-1)
                exit(0)
    for r in range(n):
        for c in range(n):
            ans[r][c]+=pow(2,i)*max(0,ansi[r][c])
for a in ans:
    print(' '.join(map(str,a)))