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

Solution in C (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <inttypes.h>

static uint32_t x=123456789;
static uint32_t y=362436069;
static uint32_t z=521288629;
static uint32_t w=88675123;

void seed(uint32_t a,uint32_t b,uint32_t c,uint32_t d) {
	if((a|b|c|d)==0)return;
	x=a;y=b;z=c;w=d;
}

uint32_t randint(void) {
	uint32_t t;
	t=(x^(x<<11));
	x=y;y=z;z=w;
	w=(w^(w>>19))^(t^(t>>8));
	return w;
}

uint64_t randint64(void) {
	return ((uint64_t)randint() << 32) | randint();
}

int N;
int S[512];
int T[512];
uint64_t U[512];
uint64_t V[512];

uint64_t must[512][512];
uint64_t dont[512][512];

uint64_t want_row[512], want_col[512];
uint64_t notall_row[512], notall_col[512];

uint64_t test[512][512];

int main(void) {
	int i, j;
	int attempt;
	if (scanf("%d", &N) != 1) return 1;
	for (i = 0; i < N; i++) {
		if (scanf("%d", &S[i]) != 1) return 1;
	}
	for (i = 0; i < N; i++) {
		if (scanf("%d", &T[i]) != 1) return 1;
	}
	for (i = 0; i < N; i++) {
		if (scanf("%" SCNu64, &U[i]) != 1) return 1;
	}
	for (i = 0; i < N; i++) {
		if (scanf("%" SCNu64, &V[i]) != 1) return 1;
	}

	for (i = 0; i < N; i++) {
		if (S[i] == 0) {
			for (j = 0; j < N; j++) must[i][j] |= U[i];
		} else {
			for (j = 0; j < N; j++) dont[i][j] |= ~U[i];
		}
	}
	for (j = 0; j < N; j++) {
		if (T[j] == 0) {
			for (i = 0; i < N; i++) must[i][j] |= V[j];
		} else {
			for (i = 0; i < N; i++) dont[i][j] |= ~V[j];
		}
	}

	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++) {
			if (must[i][j] & dont[i][j]) {
				puts("-1");
				return 0;
			}
		}
	}

	for (i = 0; i < N; i++) {
		if (S[i] == 0) notall_row[i] = ~U[i]; else want_row[i] = U[i];
		for (j = 0; j < N; j++) {
			want_row[i] &= ~must[i][j];
			notall_row[i] &= ~dont[i][j];
		}
	}

	for (j = 0; j < N; j++) {
		if (T[j] == 0) notall_col[j] = ~V[i]; else want_col[j] = V[i];
		for (i = 0; i < N; i++) {
			want_col[j] &= ~must[i][j];
			notall_col[j] &= ~dont[i][j];
		}
	}

	for (attempt = 0; attempt < 10000; attempt++) {
		int ok = 1;
		for (i = 0; i < N; i++) {
			for (j = 0; j < N; j++) test[i][j] = (randint64() | must[i][j]) & ~dont[i][j];
		}
		for (i = 0; ok && i < N; i++) {
			uint64_t ando = test[i][0], oa = test[i][0];
			for (j = 0; j < N; j++) {
				ando &= test[i][j];
				oa |= test[i][j];
			}
			if ((S[i] == 0 ? ando : oa) != U[i]) ok = 0;
		}
		for (j = 0; ok && j < N; j++) {
			uint64_t ando = test[0][j], oa = test[0][j];
			for (i = 0; i < N; i++) {
				ando &= test[i][j];
				oa |= test[i][j];
			}
			if ((T[j] == 0 ? ando : oa) != V[j]) ok = 0;
		}
		if (ok) {
			for (i = 0; i < N; i++) {
				printf("%" PRIu64, test[i][0]);
				for (j = 1; j < N; j++) printf(" %" PRIu64, test[i][j]);
				putchar('\n');
			}
			return 0;
		}
	}

	puts("-1");
	return 0;
}

/*

論理積制約 → 少なくともそれが全部1、「補集合が全部1」になってはいけない
論理和制約 → 少なくとも補集合が全部0、「それが全部0」になってはいけない

*/