AtCoder Beginner Contest 164 - I hate Matrix Construction

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-----
2
0 1
1 0
1 1
1 0

-----Sample Output-----
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.

Source: atcoder | Difficulty: MEDIUM_HARD


Solutions:
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()))
assert len(s) == len(t) == n
assert len(u) == len(v) == n
nbits = 64
AND = 0
OR = 1
A = [[u[i]] * n for i in range(n)]
for i in range(n):
	for j in range(n):
		if t[j] == AND:
			A[i][j] |= v[j]
		else:
			A[i][j] &= v[j]
		if s[i] == AND and t[j] == OR and u[i] & ~v[j] or (s[i] == OR and t[j] == AND and ~u[i] & v[j]):
			print(-1)
			exit()

def must_set(s, u, ucount, i, k):
	return s[i] == OR and u[i] & 1 << k and (ucount[i] == 0)

def must_clear(s, u, ucount, i, k):
	return s[i] == AND and u[i] & 1 << k == 0 and (ucount[i] == n)

def set_pos(t, v, vcount, k):
	for j in range(n):
		if t[j] == AND and v[j] & 1 << k == 0 and (vcount[j] < n - 1):
			return j
	raise Exception

def clear_pos(t, v, vcount, k):
	for j in range(n):
		if t[j] == OR and v[j] & 1 << k and (vcount[j] > 1):
			return j
	raise Exception

def check_spec(s, t, u, v, ucount, vcount, i, k, trans):
	if must_set(s, u, ucount, i, k):
		j = set_pos(t, v, vcount, k)
		ucount[i] += 1
		vcount[j] += 1
		if trans:
			(i, j) = (j, i)
		A[i][j] |= 1 << k
	elif must_clear(s, u, ucount, i, k):
		j = clear_pos(t, v, vcount, k)
		ucount[i] -= 1
		vcount[j] -= 1
		if trans:
			(i, j) = (j, i)
		A[i][j] &= ~(1 << k)
for k in range(nbits):
	ucount = [0] * n
	vcount = [0] * n
	for i in range(n):
		for j in range(n):
			if A[i][j] & 1 << k:
				ucount[i] += 1
				vcount[j] += 1
	try:
		for i in range(n):
			check_spec(s, t, u, v, ucount, vcount, i, k, False)
		for j in range(n):
			check_spec(t, s, v, u, vcount, ucount, j, k, True)
	except:
		print(-1)
		exit()
for i in range(n):
	print(' '.join(map(str, A[i])))


import sys
sr = lambda : sys.stdin.readline().rstrip()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))

def resolve():
	N = ir()
	S = lr()
	T = lr()
	U = lr()
	V = lr()
	a = [[0] * N for i in range(N)]
	for k in range(64):
		val = [[bool(U[i] & 1 << k), bool(V[i] & 1 << k)] for i in range(N)]
		d = [[-1] * N for i in range(N)]
		for i in range(N):
			x = val[i][0]
			if S[i] != x:
				for j in range(N):
					if d[i][j] != x and d[i][j] != -1:
						print(-1)
						return
					d[i][j] = x
		for j in range(N):
			x = val[j][1]
			if T[j] != x:
				for i in range(N):
					if d[i][j] != x and d[i][j] != -1:
						print(-1)
						return
					d[i][j] = x
		for _ in range(2):
			for i in range(N):
				x = val[i][0]
				if S[i] == x:
					p = []
					ok = False
					for j in range(N):
						if d[i][j] == -1:
							p.append(j)
						if d[i][j] == x:
							ok = True
					if ok:
						continue
					if len(p) == 0:
						print(-1)
						return
					if len(p) == 1:
						d[i][p[0]] = x
			for j in range(N):
				x = val[j][1]
				if T[j] == x:
					p = []
					ok = False
					for i in range(N):
						if d[i][j] == -1:
							p.append(i)
						if d[i][j] == x:
							ok = True
					if ok:
						continue
					if len(p) == 0:
						print(-1)
						return
					if len(p) == 1:
						d[p[0]][j] = x
		ni = []
		nj = []
		for i in range(N):
			filled = True
			for j in range(N):
				if d[i][j] == -1:
					filled = False
					break
			if not filled:
				ni.append(i)
		for j in range(N):
			filled = True
			for i in range(N):
				if d[i][j] == -1:
					filled = False
					break
			if not filled:
				nj.append(j)
		for (i, ii) in enumerate(ni):
			for (j, jj) in enumerate(nj):
				d[ii][jj] = (i + j) % 2
		for i in range(N):
			for j in range(N):
				a[i][j] |= d[i][j] << k
	for i in range(N):
		print(*a[i], sep=' ')
resolve()


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

def invalid_exit():
	print(-1)
	exit()
ans = [[0] * N for _ in range(N)]
for k in range(64):
	tmp = [[-1] * N for _ in range(N)]
	row_has_0 = [0] * N
	col_has_0 = [0] * N
	row_has_1 = [0] * N
	col_has_1 = [0] * N

	def fill(i, j, bit):
		if bit == 1 - tmp[i][j]:
			invalid_exit()
		tmp[i][j] = bit
		if bit:
			row_has_1[i] = col_has_1[j] = 1
		else:
			row_has_0[i] = col_has_0[j] = 1
	blank_rows = set(range(N))
	blank_cols = set(range(N))
	for (i, (s, u)) in enumerate(zip(S, U)):
		x = u >> k & 1
		if s == 0 and x:
			for j in range(N):
				fill(i, j, 1)
			blank_rows.remove(i)
		elif s and x == 0:
			for j in range(N):
				fill(i, j, 0)
			blank_rows.remove(i)
	for (j, (t, v)) in enumerate(zip(T, V)):
		x = v >> k & 1
		if t == 0 and x:
			for i in range(N):
				fill(i, j, 1)
			blank_cols.remove(j)
		elif t and x == 0:
			for i in range(N):
				fill(i, j, 0)
			blank_cols.remove(j)
	if blank_rows or blank_cols:
		j = list(blank_cols)[0] if len(blank_cols) == 1 else None
		for (i, (s, u)) in enumerate(zip(S, U)):
			x = u >> k & 1
			if s == x:
				if s == 0 and (not row_has_0[i]) and (len(blank_cols) == 0):
					invalid_exit()
				if s == 1 and (not row_has_1[i]) and (len(blank_cols) == 0):
					invalid_exit()
				if j is not None:
					y = V[j] >> k & 1
					if x == y:
						fill(i, j, x)
					else:
						if x == 0 and (not row_has_0[i]) and (not col_has_1[j]) and (len(blank_rows) < 2):
							invalid_exit()
						if x == 1 and (not row_has_1[i]) and (not col_has_0[j]) and (len(blank_rows) < 2):
							invalid_exit()
						if row_has_0[i]:
							fill(i, j, 1)
						else:
							fill(i, j, 0)
					blank_rows.remove(i)
		if j is not None:
			blank_cols = set()
	if blank_rows or blank_cols:
		i = list(blank_rows)[0] if len(blank_rows) == 1 else None
		for (j, (t, v)) in enumerate(zip(T, V)):
			x = v >> k & 1
			if t == x:
				if t == 0 and (not col_has_0[j]) and (len(blank_rows) == 0):
					invalid_exit()
				if t == 1 and (not col_has_1[j]) and (len(blank_rows) == 0):
					invalid_exit()
				if i is not None:
					y = U[i] >> k & 1
					if x == y:
						fill(i, j, x)
					else:
						if x == 0 and (not col_has_0[i]) and (not row_has_1[j]) and (len(blank_cols) < 2):
							invalid_exit()
						if x == 1 and (not col_has_1[i]) and (not row_has_0[j]) and (len(blank_cols) < 2):
							invalid_exit()
						if col_has_0[i]:
							fill(i, j, 1)
						else:
							fill(i, j, 0)
					blank_cols.remove(j)
		if i is not None:
			blank_rows = set()
	assert len(blank_rows) != 1
	assert len(blank_cols) != 1
	for (i, r) in enumerate(blank_rows):
		for (j, c) in enumerate(blank_cols):
			tmp[r][c] = i % 2 ^ j % 2
	for (i, row) in enumerate(tmp):
		for (j, c) in enumerate(row):
			if c == -1:
				invalid_exit()
			if c:
				ans[i][j] |= 1 << k
for row in ans:
	print(*row)


from sys import stdin
import sys
N = int(stdin.readline())
S = list(map(int, stdin.readline().split()))
T = list(map(int, stdin.readline().split()))
U = list(map(int, stdin.readline().split()))
V = list(map(int, stdin.readline().split()))
ans = [[0] * N for i in range(N)]
for dig in range(64):
	nd = 2 ** dig
	a = [[None] * N for i in range(N)]
	h = []
	w = []
	for i in range(N):
		if S[i] == 0 and U[i] & nd > 0:
			for j in range(N):
				if a[i][j] == 0:
					print(-1)
					sys.exit()
				a[i][j] = 1
		elif S[i] == 1 and U[i] & nd == 0:
			for j in range(N):
				if a[i][j] == 1:
					print(-1)
					sys.exit()
				a[i][j] = 0
		elif S[i] == 0 and U[i] & nd == 0:
			h.append((i, 0))
		else:
			h.append((i, 1))
	for j in range(N):
		if T[j] == 0 and V[j] & nd > 0:
			for i in range(N):
				if a[i][j] == 0:
					print(-1)
					sys.exit()
				a[i][j] = 1
		elif T[j] == 1 and V[j] & nd == 0:
			for i in range(N):
				if a[i][j] == 1:
					print(-1)
					sys.exit()
				a[i][j] = 0
		elif T[j] == 0 and V[j] & nd == 0:
			w.append((j, 0))
		else:
			w.append((j, 1))
	if len(h) > 1 and len(w) > 1:
		lside = 0
		now = 0
		for (i, tmp1) in h:
			now = lside
			lside ^= 1
			for (j, tmp2) in w:
				a[i][j] = now
				now ^= 1
	elif len(h) == 0 or len(w) == 0:
		for (i, x) in h:
			for j in range(N):
				if a[i][j] == x:
					break
			else:
				print(-1)
				sys.exit()
		for (j, x) in w:
			for i in range(N):
				if a[i][j] == x:
					break
			else:
				print(-1)
				sys.exit()
	elif len(w) == 1:
		wj = w[0][0]
		for (i, x) in h:
			for j in range(N):
				if a[i][j] == x:
					break
			else:
				a[i][wj] = x
		for (j, x) in w:
			flag = False
			for i in range(N):
				if a[i][j] == None:
					a[i][j] = x
				if a[i][j] == x:
					flag = True
			if not flag:
				print(-1)
				sys.exit()
	else:
		hi = h[0][0]
		for (j, x) in w:
			for i in range(N):
				if a[i][j] == x:
					break
			else:
				a[hi][j] = x
		for (i, x) in h:
			flag = False
			for j in range(N):
				if a[i][j] == None:
					a[i][j] = x
				if a[i][j] == x:
					flag = True
			if not flag:
				print(-1)
				sys.exit()
	for i in range(N):
		for j in range(N):
			ans[i][j] += nd * a[i][j]
for i in ans:
	print(*i)


import sys
sys.setrecursionlimit(10 ** 7)
from pprint import pprint as pp
from pprint import pformat as pf
import math
import bisect

class Solver:

	def __init__(self, n, s_list, t_list, u_list, v_list):
		self.n = n
		self.s_list = s_list
		self.t_list = t_list
		self.u_list = u_list
		self.v_list = v_list
		self.size = 64
		self.bit_key = 1
		self.mode = 'l'
		self.ans = self.make_ans()

	def make_ans(self):
		ans = [None] * self.n
		for l in range(self.n):
			ans[l] = [0] * self.n
		return ans

	def print_ans(self):
		for l in range(self.n):
			for c in range(self.n):
				print(self.ans[l][c], end=' ')
			print('')

	def add_straight(self, key, value):
		if not value:
			return
		if self.mode == 'l':
			for c in range(self.n):
				self.ans[key][c] |= self.bit_key
		else:
			for l in range(self.n):
				self.ans[l][key] |= self.bit_key

	def add(self, key, sub_key, value):
		if not value:
			return
		if self.mode == 'l':
			(l, c) = (key, sub_key)
		else:
			(l, c) = (sub_key, key)
		self.ans[l][c] |= self.bit_key

	def run(self):
		try:
			for bit in range(self.size):
				self.bit_key = 1 << bit
				self.solve_for_bit()
		except RuntimeError as err:
			return False
		return True

	def solve_for_bit(self):
		self.mode = 'l'
		(filled_l, ambiguous_l) = self.determine(self.s_list, self.u_list)
		self.mode = 'c'
		(filled_c, ambiguous_c) = self.determine(self.t_list, self.v_list)
		if filled_l[0] and filled_c[1] or (filled_l[1] and filled_c[0]):
			raise RuntimeError
		if len(ambiguous_l) == 1:
			self.handle_straight('l', filled_l, ambiguous_l, filled_c, ambiguous_c)
		elif len(ambiguous_c) == 1:
			self.handle_straight('c', filled_c, ambiguous_c, filled_l, ambiguous_l)
		else:
			self.checker_fill(ambiguous_l, ambiguous_c)

	def determine(self, l1, l2):
		filled = [False, False]
		ambiguous = []
		for key in range(self.n):
			st = l1[key]
			uv = 1 if l2[key] & self.bit_key else 0
			if st == uv:
				ambiguous.append((key, uv))
				continue
			filled[uv] = True
			self.add_straight(key, uv)
		return (filled, ambiguous)

	def handle_straight(self, mode, filled, ambiguous, sub_filled, sub_ambiguous):
		self.mode = mode
		(key, wanted) = ambiguous[0]
		ok = False
		if sub_filled[wanted]:
			ok = True
		for (sub_key, sub_wanted) in sub_ambiguous:
			if filled[sub_wanted]:
				self.add(key, sub_key, wanted)
				ok = True
			else:
				self.add(key, sub_key, sub_wanted)
				if wanted == sub_wanted:
					ok = True
		if not ok:
			raise RuntimeError

	def checker_fill(self, ambiguous_l, ambiguous_c):
		self.mode = 'l'
		for (il, (l_key, _)) in enumerate(ambiguous_l):
			for (ic, (c_key, _)) in enumerate(ambiguous_c):
				v = (il + ic) % 2 == 0
				self.add(l_key, c_key, v)
n = int(input())
s_list = list(map(int, input().split()))
t_list = list(map(int, input().split()))
u_list = list(map(int, input().split()))
v_list = list(map(int, input().split()))
solver = Solver(n, s_list, t_list, u_list, v_list)
ans = solver.run()
if ans is False:
	print(-1)
else:
	solver.print_ans()


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 = 64
	n = II()
	ss = LI()
	tt = LI()
	uu = LI()
	vv = LI()
	bit = [[[-1] * n for _ in range(n)] for _ in range(mxk)]
	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
	for k in range(mxk):
		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)
		if not ii:
			continue
		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]
		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]
		else:
			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))
			for (i, j) in zip(ii, jj):
				bit[k][i][j] = 1
	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()


import sys
input = sys.stdin.readline
N = int(input())
S = tuple(map(int, input().split()))
T = tuple(map(int, input().split()))
U = tuple(map(int, input().split()))
V = tuple(map(int, input().split()))
ANS = [[[-1] * N for i in range(N)] for b in range(64)]
for b in range(64):
	for i in range(N):
		if S[i] == 0 and 1 << b & U[i] != 0:
			for j in range(N):
				if ANS[b][i][j] == 0:
					print(-1)
					sys.exit()
				ANS[b][i][j] = 1
		if S[i] == 1 and 1 << b & U[i] == 0:
			for j in range(N):
				if ANS[b][i][j] == 1:
					print(-1)
					sys.exit()
				ANS[b][i][j] = 0
		if T[i] == 0 and 1 << b & V[i] != 0:
			for j in range(N):
				if ANS[b][j][i] == 0:
					print(-1)
					sys.exit()
				ANS[b][j][i] = 1
		if T[i] == 1 and 1 << b & V[i] == 0:
			for j in range(N):
				if ANS[b][j][i] == 1:
					print(-1)
					sys.exit()
				ANS[b][j][i] = 0
for rep in range(2):
	for b in range(64):
		for i in range(N):
			if S[i] == 0 and 1 << b & U[i] == 0:
				count_mi1 = 0
				okflag = 0
				for j in range(N):
					if ANS[b][i][j] == 0:
						okflag = 1
						break
					if ANS[b][i][j] == -1:
						count_mi1 += 1
						if count_mi1 >= 2:
							break
				if okflag == 0 and count_mi1 == 0:
					print(-1)
					sys.exit()
				if okflag == 0 and count_mi1 == 1:
					for j in range(N):
						if ANS[b][i][j] == -1:
							ANS[b][i][j] = 0
							break
			if S[i] == 1 and 1 << b & U[i] != 0:
				count_mi1 = 0
				okflag = 0
				for j in range(N):
					if ANS[b][i][j] == 1:
						okflag = 1
						break
					if ANS[b][i][j] == -1:
						count_mi1 += 1
						if count_mi1 >= 2:
							break
				if okflag == 0 and count_mi1 == 0:
					print(-1)
					sys.exit()
				if okflag == 0 and count_mi1 == 1:
					for j in range(N):
						if ANS[b][i][j] == -1:
							ANS[b][i][j] = 1
							break
			if T[i] == 0 and 1 << b & V[i] == 0:
				count_mi1 = 0
				okflag = 0
				for j in range(N):
					if ANS[b][j][i] == 0:
						okflag = 1
						break
					if ANS[b][j][i] == -1:
						count_mi1 += 1
						if count_mi1 >= 2:
							break
				if okflag == 0 and count_mi1 == 0:
					print(-1)
					sys.exit()
				if okflag == 0 and count_mi1 == 1:
					for j in range(N):
						if ANS[b][j][i] == -1:
							ANS[b][j][i] = 0
							break
			if T[i] == 1 and 1 << b & V[i] != 0:
				count_mi1 = 0
				okflag = 0
				for j in range(N):
					if ANS[b][j][i] == 1:
						okflag = 1
						break
					if ANS[b][j][i] == -1:
						count_mi1 += 1
						if count_mi1 >= 2:
							break
				if okflag == 0 and count_mi1 == 0:
					print(-1)
					sys.exit()
				if okflag == 0 and count_mi1 == 1:
					for j in range(N):
						if ANS[b][j][i] == -1:
							ANS[b][j][i] = 1
							break
for i in range(N):
	for j in range(N):
		S = []
		for b in range(63, -1, -1):
			if ANS[b][i][j] == -1:
				S.append((i + j) % 2)
			else:
				S.append(ANS[b][i][j])
		print(int(''.join(map(str, S)), 2), end=' ')
	print()


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()))
assert len(s) == len(t) == n
assert len(u) == len(v) == n
nbits = 64
AND = 0
OR = 1
A = [[u[i]] * n for i in range(n)]
for i in range(n):
	for j in range(n):
		if t[j] == AND:
			A[i][j] |= v[j]
		else:
			A[i][j] &= v[j]
		if s[i] == AND and t[j] == OR and u[i] & ~v[j] or (s[i] == OR and t[j] == AND and ~u[i] & v[j]):
			print(-1)
			exit()
ucount = [[0] * nbits for i in range(n)]
vcount = [[0] * nbits for j in range(n)]
for i in range(n):
	for j in range(n):
		for k in range(nbits):
			if A[i][j] & 1 << k:
				ucount[i][k] += 1
				vcount[j][k] += 1
for k in range(nbits):
	mask = 1 << k
	for i in range(n):
		if s[i] == OR and u[i] & 1 << k and (ucount[i][k] == 0):
			for j in range(n):
				if t[j] == OR and v[j] & mask or (t[j] == AND and v[j] & mask == 0 and (vcount[j][k] < n - 1)):
					A[i][j] |= mask
					ucount[i][k] += 1
					vcount[j][k] += 1
					break
			else:
				print(-1)
				exit()
		if s[i] == AND and u[i] & mask == 0 and (ucount[i][k] == n):
			for j in range(n):
				if t[j] == AND and v[j] & mask == 0 or (t[j] == OR and v[j] & mask and (vcount[j][k] > 1)):
					A[i][j] &= ~mask
					ucount[i][k] -= 1
					vcount[j][k] -= 1
					break
			else:
				print(-1)
				exit()
	for j in range(n):
		if t[j] == OR and v[j] & mask and (vcount[j][k] == 0):
			for i in range(n):
				if s[i] == OR and u[i] & mask or (s[i] == AND and u[i] & mask == 0 and (ucount[i][k] < n - 1)):
					A[i][j] |= mask
					ucount[i][k] += 1
					vcount[j][k] += 1
					break
			else:
				print(-1)
				exit()
		if t[j] == AND and v[j] & mask == 0 and (vcount[j][k] == n):
			for i in range(n):
				if s[i] == AND and u[i] & mask == 0 or (s[i] == OR and u[i] & mask and (ucount[i][k] > 1)):
					A[i][j] &= ~mask
					ucount[i][k] -= 1
					vcount[j][k] -= 1
					break
			else:
				print(-1)
				exit()
for i in range(n):
	print(' '.join(map(str, A[i])))


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):
	ansi = [[-1] * n for _ in range(n)]
	for r in range(n):
		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])):
			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])):
					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])):
			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])):
					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)])):
			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])):
					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)])):
			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])):
					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)))


from functools import reduce
from operator import and_, or_
N = int(input())
(S, T, U, V) = [[int(a) for a in input().split()] for _ in range(4)]
X = [[U[i] & V[j] if S[i] and T[j] else U[i] if T[j] else V[j] if S[i] else U[i] | V[j] for j in range(N)] for i in range(N)]
for _ in range(2):
	A1 = [-1] * N
	A2 = [[-1] * N for _ in range(N)]
	for i in range(N - 1)[::-1]:
		for j in range(N):
			A2[i][j] = A2[i + 1][j] & X[i + 1][j]
	for i in range(N):
		a = ~U[i] | reduce(or_, X[i])
		for j in range(N):
			if S[i] and (not T[j]):
				X[i][j] |= ~(A1[j] & A2[i][j] | a)
				a |= X[i][j]
			A1[j] &= X[i][j]
	if [reduce(or_ if S[i] else and_, x) for (i, x) in enumerate(X)] != U:
		print(-1)
		exit()
	X = [[X[j][i] for j in range(N)] for i in range(N)]
	(S, T, U, V) = (T, S, V, U)
for x in X:
	print(*x)


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()))
assert len(s) == len(t) == n
assert len(u) == len(v) == n
nbits = 64
AND = 0
OR = 1
for i in range(n):
	for j in range(n):
		if s[i] == AND and t[j] == OR and u[i] & ~v[j] or (s[i] == OR and t[j] == AND and ~u[i] & v[j]):
			print(-1)
			exit()
A = [[u[i]] * n for i in range(n)]
for i in range(n):
	for j in range(n):
		if t[j] == AND:
			A[i][j] |= v[j]
		else:
			A[i][j] &= v[j]
ucount = [[0] * nbits for i in range(n)]
vcount = [[0] * nbits for j in range(n)]
for i in range(n):
	for j in range(n):
		for k in range(nbits):
			if A[i][j] & 1 << k:
				ucount[i][k] += 1
				vcount[j][k] += 1

def must_set(s, u, ucount, i, k):
	return s[i] == OR and u[i] & 1 << k and (ucount[i][k] == 0)

def must_clear(s, u, ucount, i, k):
	return s[i] == AND and u[i] & 1 << k == 0 and (ucount[i][k] == n)

def find_and_set(t, v, vcount, k):
	for j in range(n):
		if t[j] == OR and v[j] & 1 << k or (t[j] == AND and v[j] & 1 << k == 0 and (vcount[j][k] < n - 1)):
			return j
	raise Exception

def find_and_clear(t, v, vcount, k):
	for j in range(n):
		if t[j] == AND and v[j] & 1 << k == 0 or (t[j] == OR and v[j] & 1 << k and (vcount[j][k] > 1)):
			return j
	raise Exception
try:
	for i in range(n):
		for k in range(nbits):
			if must_set(s, u, ucount, i, k):
				j = find_and_set(t, v, vcount, k)
				A[i][j] |= 1 << k
				ucount[i][k] += 1
				vcount[j][k] += 1
			elif must_clear(s, u, ucount, i, k):
				j = find_and_clear(t, v, vcount, k)
				A[i][j] &= ~(1 << k)
				ucount[i][k] -= 1
				vcount[j][k] -= 1
	for j in range(n):
		for k in range(nbits):
			if must_set(t, v, vcount, j, k):
				i = find_and_set(s, u, ucount, k)
				A[i][j] |= 1 << k
				ucount[i][k] += 1
				vcount[j][k] += 1
			elif must_clear(t, v, vcount, j, k):
				i = find_and_clear(s, u, ucount, k)
				A[i][j] &= ~(1 << k)
				ucount[i][k] -= 1
				vcount[j][k] -= 1
except:
	print(-1)
	exit()
for i in range(n):
	print(' '.join(map(str, A[i])))


N = int(input())
S = [int(a) for a in input().split()]
T = [int(a) for a in input().split()]
U = [int(a) for a in input().split()]
V = [int(a) for a in input().split()]
X = [[0] * N for _ in range(N)]
m = (1 << 64) - 1
for i in range(N):
	for j in range(N):
		if not S[i]:
			X[i][j] |= U[i]
		if not T[j]:
			X[i][j] |= V[j]
		X[i][j] |= U[i] & V[j]
A1 = [[m] * N for _ in range(N)]
A2 = [[m] * N for _ in range(N)]
for i in range(N - 1)[::-1]:
	for j in range(N):
		A2[i][j] = A2[i + 1][j] & X[i + 1][j]
for i in range(N):
	a = 0
	for j in range(N):
		a |= X[i][j]
	for j in range(N):
		if i:
			A1[i][j] = A1[i - 1][j] & X[i - 1][j]
		if S[i] and (not T[j]):
			X[i][j] |= U[i] & ~(A1[i][j] & A2[i][j] | a)
		a |= X[i][j]
A1 = [[m] * N for _ in range(N)]
A2 = [[m] * N for _ in range(N)]
for j in range(N - 1)[::-1]:
	for i in range(N):
		A2[i][j] = A2[i][j + 1] & X[i][j + 1]
for j in range(N):
	a = 0
	for i in range(N):
		a |= X[i][j]
	for i in range(N):
		if j:
			A1[i][j] = A1[i][j - 1] & X[i][j - 1]
		if T[j] and (not S[i]):
			X[i][j] |= V[j] & ~(A1[i][j] & A2[i][j] | a)
		a |= X[i][j]
for i in range(N):
	if S[i] == 0:
		s = m
		for j in range(N):
			s &= X[i][j]
		if s != U[i]:
			print(-1)
			exit()
	else:
		s = 0
		for j in range(N):
			s |= X[i][j]
		if s != U[i]:
			print(-1)
			exit()
for j in range(N):
	if T[j] == 0:
		s = m
		for i in range(N):
			s &= X[i][j]
		if s != V[j]:
			print(-1)
			exit()
	else:
		s = 0
		for i in range(N):
			s |= X[i][j]
		if s != V[j]:
			print(-1)
			exit()
for x in X:
	print(*x)


(n, *s) = map(int, open(0).read().split())
(s, t, f, g) = [s[i * n:i * n + n] for i in range(4)]
r = range(n)
a = [n * [0] for _ in r]
for b in range(64):
	e = 1 << b
	u = [k & e for k in f]
	v = [k & e for k in g]
	l = [0] * n
	m = l[:]
	for i in r:
		for j in r:
			if u[i] & v[j] | (s[i] ^ 1) * u[i] | (t[j] ^ 1) * v[j]:
				a[i][j] |= e
				l[i] += 1
				m[j] += 1
	for i in r:
		if (l[i] == 0) * s[i] * u[i]:
			for j in r:
				if (t[j] ^ 1) * (n - m[j] > 1):
					a[i][j] |= e
					m[j] += 1
					break
		if (m[i] == 0) * t[i] * v[i]:
			for j in r:
				if (s[j] ^ 1) * (n - l[j] > 1):
					a[j][i] |= e
					l[j] += 1
					break
h = a[0][:]
w = [i[0] for i in a]
for i in r:
	for j in r:
		w[i] = w[i] | a[i][j] if s[i] else w[i] & a[i][j]
		h[j] = h[j] | a[i][j] if t[j] else h[j] & a[i][j]
if (g != h) | (f != w):
	print(-1)
	exit()
for i in a:
	print(*i)


import sys
from functools import reduce
from operator import or_, and_

def solve(N: int, S: 'List[int]', T: 'List[int]', U: 'List[int]', V: 'List[int]'):
	(AND, ORE) = (0, 1)
	ans = [[0 for j in range(N)] for i in range(N)]
	can = True
	for b in range(64):
		parityU = [x >> b & 1 for x in U]
		parityV = [x >> b & 1 for x in V]
		can &= all((not (S[i] == ORE and parityU[i] == 0 and (T[j] == AND) and (parityV[j] == 1)) for i in range(N) for j in range(N)))
		can &= all((not (S[i] == AND and parityU[i] == 1 and (T[j] == ORE) and (parityV[j] == 0)) for i in range(N) for j in range(N)))
		for i in range(N):
			if S[i] == AND and parityU[i] == 1:
				for j in range(N):
					ans[i][j] |= 1 << b
		for j in range(N):
			if T[j] == AND and parityV[j] == 1:
				for i in range(N):
					ans[i][j] |= 1 << b
		rows_free0 = [i for i in range(N) if parityU[i] == 0 or (S[i] == ORE and any((x == 1 for x in parityV)))]
		rows_free1 = [i for i in range(N) if parityU[i] == 1 or (S[i] == AND and any((x == 0 for x in parityV)))]
		cols_free0 = [j for j in range(N) if parityV[j] == 0 or (T[j] == ORE and any((x == 1 for x in parityU)))]
		cols_free1 = [j for j in range(N) if parityV[j] == 1 or (T[j] == AND and any((x == 0 for x in parityU)))]
		rows_need0 = [i for i in range(N) if S[i] == AND and parityU[i] == 0] if not cols_free0 else []
		rows_need1 = [i for i in range(N) if S[i] == ORE and parityU[i] == 1]
		cols_need0 = [j for j in range(N) if T[j] == AND and parityV[j] == 0] if not rows_free0 else []
		cols_need1 = [j for j in range(N) if T[j] == ORE and parityV[j] == 1]
		if rows_free1:
			for col in cols_need1:
				ans[rows_free1[0]][col] |= 1 << b
			cols_need1 = []
		if cols_free1:
			for row in rows_need1:
				ans[row][cols_free1[0]] |= 1 << b
			rows_need1 = []
		can &= not rows_need0 or any((T[j] == ORE for j in range(N)))
		can &= not rows_need1 or any((T[j] == AND for j in range(N)))
		can &= not cols_need0 or any((S[i] == ORE for i in range(N)))
		can &= not cols_need1 or any((S[i] == AND for i in range(N)))
		can &= not any((len(a) == 1 and len(b) > 0 or (len(b) == 1 and len(a) > 0) for (a, b) in ((rows_need0, cols_need1), (rows_need1, cols_need0))))
		for (rows, cols) in ((rows_need0, cols_need1), (rows_need1, cols_need0)):
			for (ii, i) in enumerate(rows):
				for (jj, j) in enumerate(cols):
					ans[i][j] |= (ii & 1 ^ jj & 1) << b
	if can:
		for row in ans:
			print(*row)
		assert all((reduce(or_ if S[i] else and_, (ans[i][j] for j in range(N))) == U[i] for i in range(N)))
		assert all((reduce(or_ if T[j] else and_, (ans[i][j] for i in range(N))) == V[j] for j in range(N)))
	else:
		print(-1)

def main():
	N = int(sys.stdin.readline())
	S = [int(x) for x in sys.stdin.readline().split()]
	T = [int(x) for x in sys.stdin.readline().split()]
	U = [int(x) for x in sys.stdin.readline().split()]
	V = [int(x) for x in sys.stdin.readline().split()]
	solve(N, S, T, U, V)
from time import time
start = time()
main()
end = time()
print(end - start, file=sys.stderr)


def main():
	import sys
	sys.setrecursionlimit(10 ** 9)
	input = sys.stdin.readline
	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()))
	range_N = range(N)
	ans = [[0] * N for _ in range_N]
	for i in range_N:
		if S[i] == 0:
			for j in range_N:
				ans[i][j] = ans[i][j] | U[i]
		if T[i] == 0:
			for j in range_N:
				ans[j][i] = ans[j][i] | V[i]
	for i in range_N:
		for j in range_N:
			if U[i] & V[j]:
				ans[i][j] = ans[i][j] | U[i] & V[j]
	for x in range_N:
		if S[x] == 0:
			continue
		x_sum = ans[x][0]
		for y in range_N:
			x_sum = x_sum | ans[x][y]
		if x_sum == U[x]:
			continue
		up = U[x] - x_sum
		for y in range_N:
			if T[y]:
				continue
			y_mul = ans[0][y]
			for i in range_N:
				if i == x:
					continue
				y_mul = y_mul & ans[i][y]
			up_y = ~y_mul & up
			ans[x][y] += up_y
			up -= up_y
			if up == 0:
				break
	for y in range_N:
		if T[y] == 0:
			continue
		y_sum = ans[0][y]
		for x in range_N:
			y_sum = y_sum | ans[x][y]
		if y_sum == V[y]:
			continue
		up = V[y] - y_sum
		for x in range_N:
			if S[x]:
				continue
			x_mul = ans[x][0]
			for j in range_N:
				if y == j:
					continue
				x_mul = x_mul & ans[x][j]
			up_x = ~x_mul & up
			ans[x][y] += up_x
			up -= up_x
			if up == 0:
				break
	for i in range_N:
		check_xs = ans[i][0]
		check_ys = ans[0][i]
		check_xm = ans[i][0]
		check_ym = ans[0][i]
		for j in range_N:
			check_xs = check_xs | ans[i][j]
			check_ys = check_ys | ans[j][i]
			check_xm = check_xm & ans[i][j]
			check_ym = check_ym & ans[j][i]
		if S[i] and U[i] != check_xs or (T[i] and V[i] != check_ys) or (S[i] == 0 and U[i] != check_xm) or (T[i] == 0 and V[i] != check_ym):
			print(-1)
			return
	for i in range_N:
		print(*ans[i])
main()


import sys
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
N = int(input())
S = tuple(map(int, input().split()))
T = tuple(map(int, input().split()))
U = tuple(map(int, input().split()))
V = tuple(map(int, input().split()))
ANS = [[[-1] * N for i in range(N)] for b in range(64)]
for b in range(64):
	for i in range(N):
		if S[i] == 0 and 1 << b & U[i] != 0:
			for j in range(N):
				if ANS[b][i][j] == 0:
					print(-1)
					sys.exit()
				ANS[b][i][j] = 1
		if S[i] == 1 and 1 << b & U[i] == 0:
			for j in range(N):
				if ANS[b][i][j] == 1:
					print(-1)
					sys.exit()
				ANS[b][i][j] = 0
		if T[i] == 0 and 1 << b & V[i] != 0:
			for j in range(N):
				if ANS[b][j][i] == 0:
					print(-1)
					sys.exit()
				ANS[b][j][i] = 1
		if T[i] == 1 and 1 << b & V[i] == 0:
			for j in range(N):
				if ANS[b][j][i] == 1:
					print(-1)
					sys.exit()
				ANS[b][j][i] = 0
for rep in range(2):
	for b in range(64):
		for i in range(N):
			if S[i] == 0 and 1 << b & U[i] == 0:
				count_mi1 = 0
				okflag = 0
				for j in range(N):
					if ANS[b][i][j] == 0:
						okflag = 1
						break
					if ANS[b][i][j] == -1:
						count_mi1 += 1
						if count_mi1 >= 2:
							break
				if okflag == 0 and count_mi1 == 0:
					print(-1)
					sys.exit()
				if okflag == 0 and count_mi1 == 1:
					for j in range(N):
						if ANS[b][i][j] == -1:
							ANS[b][i][j] = 0
							break
			if S[i] == 1 and 1 << b & U[i] != 0:
				count_mi1 = 0
				okflag = 0
				for j in range(N):
					if ANS[b][i][j] == 1:
						okflag = 1
						break
					if ANS[b][i][j] == -1:
						count_mi1 += 1
						if count_mi1 >= 2:
							break
				if okflag == 0 and count_mi1 == 0:
					print(-1)
					sys.exit()
				if okflag == 0 and count_mi1 == 1:
					for j in range(N):
						if ANS[b][i][j] == -1:
							ANS[b][i][j] = 1
							break
			if T[i] == 0 and 1 << b & V[i] == 0:
				count_mi1 = 0
				okflag = 0
				for j in range(N):
					if ANS[b][j][i] == 0:
						okflag = 1
						break
					if ANS[b][j][i] == -1:
						count_mi1 += 1
						if count_mi1 >= 2:
							break
				if okflag == 0 and count_mi1 == 0:
					print(-1)
					sys.exit()
				if okflag == 0 and count_mi1 == 1:
					for j in range(N):
						if ANS[b][j][i] == -1:
							ANS[b][j][i] = 0
							break
			if T[i] == 1 and 1 << b & V[i] != 0:
				count_mi1 = 0
				okflag = 0
				for j in range(N):
					if ANS[b][j][i] == 1:
						okflag = 1
						break
					if ANS[b][j][i] == -1:
						count_mi1 += 1
						if count_mi1 >= 2:
							break
				if okflag == 0 and count_mi1 == 0:
					print(-1)
					sys.exit()
				if okflag == 0 and count_mi1 == 1:
					for j in range(N):
						if ANS[b][j][i] == -1:
							ANS[b][j][i] = 1
							break
for i in range(N):
	for j in range(N):
		S = []
		for b in range(63, -1, -1):
			if ANS[b][i][j] == -1:
				S.append((i + j) % 2)
			else:
				S.append(ANS[b][i][j])
		sys.stdout.write(str(int(''.join(map(str, S)), 2)) + ' ')
	print()


n = int(input())
s = list(map(int, input().split()))
t = list(map(int, input().split()))
f = list(map(int, input().split()))
g = list(map(int, input().split()))
ans = [n * [0] for _ in range(n)]
for b in range(64):
	e = 1 << b
	u = [a & e for a in f]
	v = [a & e for a in g]
	l = [0] * n
	m = [0] * n
	for i in range(n):
		for j in range(n):
			if u[i] & v[j] | (s[i] ^ 1) * u[i] | (t[j] ^ 1) * v[j]:
				ans[i][j] |= e
				l[i] += 1
				m[j] += 1
	for i in range(n):
		if s[i] * u[i] * (l[i] == 0):
			for j in range(n):
				if (t[j] ^ 1) * (n - m[j] > 1):
					ans[i][j] |= e
					m[j] += 1
					break
	for j in range(n):
		if t[j] * v[j] * (m[j] == 0):
			for i in range(n):
				if (s[i] ^ 1) * (n - l[i] > 1):
					ans[i][j] |= e
					l[i] += 1
					break
h = ans[0][:]
w = [i[0] for i in ans]
for i in range(n):
	for j in range(n):
		if s[i]:
			w[i] |= ans[i][j]
		else:
			w[i] &= ans[i][j]
		if t[j]:
			h[j] |= ans[i][j]
		else:
			h[j] &= ans[i][j]
if (g != h) | (f != w):
	print(-1)
	exit()
for i in ans:
	print(*i)


def disp(s, L):
	return
	print(s + ' =')
	for x in L:
		print(*x)
	print()
N = int(input())
S = [int(a) for a in input().split()]
T = [int(a) for a in input().split()]
U = [int(a) for a in input().split()]
V = [int(a) for a in input().split()]
X = [[0] * N for _ in range(N)]
m = (1 << 64) - 1
for i in range(N):
	for j in range(N):
		if not S[i]:
			X[i][j] |= U[i]
		if not T[j]:
			X[i][j] |= V[j]
		X[i][j] |= U[i] & V[j]
disp('X', X)
A1 = [[m] * N for _ in range(N)]
A2 = [[m] * N for _ in range(N)]
for i in range(N - 1)[::-1]:
	for j in range(N):
		A2[i][j] = A2[i + 1][j] & X[i + 1][j]
for i in range(N):
	a = 0
	for j in range(N):
		a |= X[i][j]
	for j in range(N):
		if i:
			A1[i][j] = A1[i - 1][j] & X[i - 1][j]
		if S[i] and (not T[j]):
			X[i][j] |= U[i] & ~(A1[i][j] & A2[i][j]) & ~a
		a |= X[i][j]
disp('X', X)
disp('A1', A1)
disp('A2', A2)
if False:
	for i in range(N):
		if not S[i]:
			continue
		A = [m] * N
		for j in range(N - 1)[::-1]:
			A[j] = A[j + 1] & X[i][j + 1]
		a = m
		for j in range(N):
			if not T[j]:
				X[i][j] |= U[i] & ~(a & A[j])
			a &= X[i][j]
A1 = [[m] * N for _ in range(N)]
A2 = [[m] * N for _ in range(N)]
for j in range(N - 1)[::-1]:
	for i in range(N):
		A2[i][j] = A2[i][j + 1] & X[i][j + 1]
for j in range(N):
	a = 0
	for i in range(N):
		a |= X[i][j]
	for i in range(N):
		if j:
			A1[i][j] = A1[i][j - 1] & X[i][j - 1]
		if T[j] and (not S[i]):
			X[i][j] |= V[j] & ~(A1[i][j] & A2[i][j]) & ~a
		a |= X[i][j]
disp('X', X)
disp('A1', A1)
disp('A2', A2)
if False:
	for j in range(N):
		if not T[j]:
			continue
		A = [m] * N
		for i in range(N - 1)[::-1]:
			A[i] = A[i + 1] & X[i + 1][j]
		a = m
		for i in range(N):
			if not S[i]:
				X[i][j] |= V[j] & ~(a & A[i])
			a &= X[i][j]
for i in range(N):
	if S[i] == 0:
		s = m
		for j in range(N):
			s &= X[i][j]
		if s != U[i]:
			print(-1)
			exit()
	else:
		s = 0
		for j in range(N):
			s |= X[i][j]
		if s != U[i]:
			print(-1)
			exit()
for j in range(N):
	if T[j] == 0:
		s = m
		for i in range(N):
			s &= X[i][j]
		if s != V[j]:
			print(-1)
			exit()
	else:
		s = 0
		for i in range(N):
			s |= X[i][j]
		if s != V[j]:
			print(-1)
			exit()
for x in X:
	print(*x)
disp('X', X)
disp('A1', A1)
disp('A2', A2)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
mod = 10 ** 9 + 7

def main():
	n = int(input())
	ans = [[0 for _ in range(n)] for _ in range(n)]
	s = list(map(int, input().split()))
	t = list(map(int, input().split()))
	u = list(map(int, input().split()))
	v = list(map(int, input().split()))
	for i in range(n):
		if not s[i]:
			for j in range(n):
				ans[i][j] = ans[i][j] | u[i]
		if not t[i]:
			for j in range(n):
				ans[j][i] = ans[j][i] | v[i]
	for i in range(n):
		for j in range(n):
			if u[i] & v[j]:
				ans[i][j] = ans[i][j] | u[i] & v[j]
	for x in range(n):
		if not s[x]:
			continue
		x_sum = ans[x][0]
		for y in range(n):
			x_sum = x_sum | ans[x][y]
		if x_sum == u[x]:
			continue
		up = u[x] - x_sum
		for y in range(n):
			if t[y]:
				continue
			y_mul = ans[0][y]
			for i in range(n):
				if i == x:
					continue
				y_mul = y_mul & ans[i][y]
			up_y = ~y_mul & up
			ans[x][y] += up_y
			up -= up_y
			if not up:
				break
	for y in range(n):
		if not t[y]:
			continue
		y_sum = ans[0][y]
		for x in range(n):
			y_sum = y_sum | ans[x][y]
		if y_sum == v[y]:
			continue
		up = v[y] - y_sum
		for x in range(n):
			if s[x]:
				continue
			x_mul = ans[x][0]
			for j in range(n):
				if y == j:
					continue
				x_mul = x_mul & ans[x][j]
			up_x = ~x_mul & up
			ans[x][y] += up_x
			up -= up_x
			if not up:
				break
	for i in range(n):
		check_xs = ans[i][0]
		check_ys = ans[0][i]
		check_xm = ans[i][0]
		check_ym = ans[0][i]
		for j in range(n):
			check_xs = check_xs | ans[i][j]
			check_ys = check_ys | ans[j][i]
			check_xm = check_xm & ans[i][j]
			check_ym = check_ym & ans[j][i]
		if s[i] and u[i] != check_xs:
			print(-1)
			return
		if t[i] and v[i] != check_ys:
			print(-1)
			return
		if not s[i] and u[i] != check_xm:
			print(-1)
			return
		if not t[i] and v[i] != check_ym:
			print(-1)
			return
	for i in range(n):
		print(*ans[i])
main()


import sys
readline = sys.stdin.readline

def check(k):
	res = [[None] * N for _ in range(N)]
	H = []
	W = []
	for i in range(N):
		if S[i] == 0 and U[i] & 1 << k:
			res[i] = [1] * N
		elif S[i] == 1 and (not U[i] & 1 << k):
			res[i] = [0] * N
		else:
			H.append(i)
	for j in range(N):
		if T[j] == 0 and V[j] & 1 << k:
			for i in range(N):
				if res[i][j] == 0:
					return -1
				res[i][j] = 1
		elif T[j] == 1 and (not V[j] & 1 << k):
			for i in range(N):
				if res[i][j] == 1:
					return -1
				res[i][j] = 0
		else:
			W.append(j)
	for st in range(4):
		if st > 1:
			for i in range(len(H)):
				for j in range(len(W)):
					(h, w) = (H[i], W[j])
					res[h][w] = (i + j + st) % 2
		else:
			for i in range(len(H)):
				for j in range(len(W)):
					(h, w) = (H[i], W[j])
					res[h][w] = st
		flag = True
		for i in H:
			if S[i] == 0:
				cnt = 1
				for j in range(N):
					cnt &= res[i][j]
				if (cnt ^ U[i] >> k) & 1:
					flag = False
					break
			else:
				cnt = 0
				for j in range(N):
					cnt |= res[i][j]
				if (cnt ^ U[i] >> k) & 1:
					flag = False
					break
		else:
			for j in W:
				if T[j] == 0:
					cnt = 1
					for i in range(N):
						cnt &= res[i][j]
					if (cnt ^ V[j] >> k) & 1:
						flag = False
						break
				else:
					cnt = 0
					for i in range(N):
						cnt |= res[i][j]
					if (cnt ^ V[j] >> k) & 1:
						flag = False
						break
		if flag:
			return res
	return -1
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 = [[0] * N for _ in range(N)]
for k in range(64):
	res = check(k)
	if res == -1:
		ans = -1
		break
	for i in range(N):
		for j in range(N):
			ans[i][j] |= res[i][j] << k
if ans == -1:
	print(-1)
else:
	print('\n'.join([' '.join(map(str, a)) for a in ans]))


from functools import reduce
import operator
N = int(input())
Ss = list(map(int, input().split()))
Ts = list(map(int, input().split()))
Us = list(map(int, input().split()))
Vs = list(map(int, input().split()))
mat = [[0] * N for _ in range(N)]
counts_r = [[0] * 64 for _ in range(N)]
counts_c = [[0] * 64 for _ in range(N)]

class NotOK(Exception):
	pass
try:
	for i in range(N):
		for j in range(N):
			x = 0
			if not Ss[i]:
				x |= Us[i]
			if not Ts[j]:
				x |= Vs[j]
			if Ss[i] and ~Us[i] & x:
				raise NotOK
			if Ts[j] and ~Vs[j] & x:
				raise NotOK
			if Ss[i] and Ts[j]:
				x |= Us[i] & Vs[j]
			mat[i][j] = x
			for k in range(64):
				if x >> k & 1:
					counts_r[i][k] += 1
					counts_c[j][k] += 1

	def corr(x, vals, ors, counts):
		for k in range(64):
			if x >> k & 1 == 0:
				continue
			for j in range(N):
				if ors[j]:
					continue
				if counts[j][k] >= N - 1:
					continue
				counts[j][k] += 1
				vals[j] += 1 << k
				x -= 1 << k
				if not x:
					return x
				break
		return x
	for i in range(N):
		vals = mat[i]
		if Ss[i]:
			x = reduce(operator.or_, vals)
			if corr(Us[i] & ~x, vals, Ts, counts_c):
				raise NotOK
		vals = []
		for j in range(N):
			vals.append(mat[j][i])
		if Ts[i]:
			x = reduce(operator.or_, vals)
			if corr(Vs[i] & ~x, vals, Ss, counts_r):
				raise NotOK
			for j in range(N):
				mat[j][i] = vals[j]
	for i in range(N):
		vals = mat[i]
		if Ss[i]:
			x = reduce(operator.or_, vals)
		else:
			x = reduce(operator.and_, vals)
		if x != Us[i]:
			raise NotOK
		vals = []
		for j in range(N):
			vals.append(mat[j][i])
		if Ts[i]:
			x = reduce(operator.or_, vals)
		else:
			x = reduce(operator.and_, vals)
		if x != Vs[i]:
			raise NotOK
	for i in range(N):
		print(*mat[i])
except NotOK:
	print(-1)


import math

def f():
	print(-1)
	exit()

def gen(p, n, s, t, u, v, result):
	a = [[-1 for i in range(n)] for j in range(n)]
	for i in range(n):
		ub = u[i] >> p & 1
		vb = v[i] >> p & 1
		if s[i] == 0:
			if ub == 1:
				a[i] = [1 for j in range(n)]
		elif ub == 0:
			a[i] = [0 for j in range(n)]
		if t[i] == 0:
			if vb == 1:
				for j in range(n):
					if a[j][i] == 0:
						f()
					a[j][i] = 1
		elif vb == 0:
			for j in range(n):
				if a[j][i] == 1:
					f()
				a[j][i] = 0
	for i in range(n):
		ub = u[i] >> p & 1
		if s[i] == 1 and ub == 1:
			if 1 in a[i]:
				continue
			for j in range(n):
				vb = v[j] >> p & 1
				if t[j] == 1 and vb == 1:
					a[i][j] = 1
					break
				if t[j] == 0 and vb == 0:
					if [a[k][j] for k in range(n)].count(1) < n - 1:
						a[i][j] = 1
						break
			else:
				f()
	for i in range(n):
		ub = u[i] >> p & 1
		if s[i] == 0 and ub == 0:
			if a[i].count(1) == n:
				f()
	for j in range(n):
		vb = v[j] >> p & 1
		if t[j] == 1 and vb == 1:
			if 1 in [a[k][j] for k in range(n)]:
				continue
			for i in range(n):
				ub = u[i] >> p & 1
				if s[i] == 1 and ub == 1:
					a[i][j] = 1
					break
				if s[i] == 0 and ub == 0:
					if a[i].count(1) < n - 1:
						a[i][j] = 1
						break
			else:
				f()
	for j in range(n):
		vb = v[j] >> p & 1
		if t[j] == 0 and vb == 0:
			if [a[k][j] for k in range(n)].count(1) == n:
				fi()
	for i in range(n):
		for j in range(n):
			v = a[i][j] if a[i][j] >= 0 else 0
			result[i][j] = result[i][j] | v << p
n = int(input())
s = tuple(map(int, input().split()))
t = tuple(map(int, input().split()))
u = tuple(map(int, input().split()))
v = tuple(map(int, input().split()))
p = math.log2(max(u + v))
if p.is_integer():
	p = int(p) + 1
else:
	p = int(math.ceil(p))
result = [[0 for i in range(n)] for j in range(n)]
for i in range(p):
	gen(i, n, s, t, u, v, result)
for i in range(n):
	print(' '.join(map(str, result[i])))


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)]
ss = [u[i] if s[i] == 0 else 0 for i in range(n)]
tt = [v[i] if t[i] == 0 else 0 for i in range(n)]
son = [[] for _ in range(n)]
ton = [[] for _ in range(n)]
sno = [[n - 1] * 64 for _ in range(n)]
tno = [[n - 1] * 64 for _ in range(n)]
for i in range(n):
	for j in range(n):
		ans[i][j] = u[i] & v[j]
		if s[i] == 0:
			ans[i][j] |= u[i]
			ss[i] &= ans[i][j]
		else:
			ss[i] |= ans[i][j]
		if t[j] == 0:
			ans[i][j] |= v[j]
			tt[j] &= ans[i][j]
		else:
			tt[j] |= ans[i][j]
		if s[i] == 0 or t[i] == 0:
			for k in range(64):
				if ans[i][j] & 1 << k:
					if s[i] == 0:
						sno[i][k] -= 1
					if t[j] == 0:
						tno[j][k] -= 1
		if j == n - 1:
			if s[i] == 1:
				if ss[i] | u[i] > u[i]:
					print(-1)
					exit()
				if ss[i] != u[i]:
					for k in range(64):
						if u[i] & 1 << k:
							if ss[i] & 1 << k == 0:
								son[i].append(k)
					ss[i] = 1
				else:
					ss[i] = 0
			elif ss[i] != u[i]:
				print(-1)
				exit()
			else:
				ss[i] = 2
		if i == n - 1:
			if t[j] == 1:
				if tt[j] | v[j] > v[j]:
					print(-1)
					exit()
				if tt[j] != v[j]:
					for k in range(64):
						if v[j] & 1 << k:
							if tt[j] & 1 << k == 0:
								ton[j].append(k)
					tt[j] = 1
				else:
					tt[j] = 0
			elif tt[j] != v[j]:
				print(-1)
				exit()
			else:
				tt[j] = 2
for i in range(n):
	for k in son[i]:
		for j in range(n):
			if t[j] == 0:
				if tno[j][k] > 0:
					ans[i][j] |= 1 << k
					tno[j][k] -= 1
					break
			if j == n - 1:
				print(-1)
				exit()
for j in range(n):
	for k in ton[j]:
		for i in range(n):
			if s[i] == 0:
				if sno[i][k] > 0:
					ans[i][j] |= 1 << k
					sno[i][k] -= 1
					break
			if i == n - 1:
				print(-1)
				exit()
for i in range(n):
	if s[i] == 0:
		uu = ans[i][0]
	else:
		uu = 0
	for j in range(n):
		if s[i] == 0:
			uu &= ans[i][j]
		else:
			uu |= ans[i][j]
	if uu != u[i]:
		print(-1)
		exit()
for j in range(n):
	if t[j] == 0:
		vv = ans[0][j]
	else:
		vv = 0
	for i in range(n):
		if t[j] == 0:
			vv &= ans[i][j]
		else:
			vv |= ans[i][j]
	if vv != v[j]:
		print(-1)
		exit()
for i in ans:
	print(*i)