I hate Matrix Construction

0

0 votes
Hard
Problem

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:

  • ai,j is an integer.
  • 0 ai,j 264.
  • If Si=0, the bitwise AND of the elements in the i-th row is Ui.
  • If Si=1, the bitwise OR of the elements in the i-th row is Ui.
  • If Ti=0, the bitwise AND of the elements in the i-th column is Vi.
  • If Ti=1, the bitwise OR of the elements in the i-th column is Vi.

However, there may be cases where no matrix satisfies the conditions.

Constraints

  • All values in input are integers.
  • 1 N 500
  • 0 Si  10
  • 0 Ti  10
  • 0 Ui < 264
  • 0 Vi < 264

Input

Input is given from Standard Input in the following format:

N
S1 S2 ...  SN
T1 T2 ...  TN
U1 U2 ...  UN
V1 V2 ...  VN

Output

If there exists a matrix that satisfies the conditions, print one such matrix in the following format:

a1,1 ...  a1,N
:
aN,1 ...  aN,N

Note that any matrix satisfying the conditions is accepted.

If no matrix satisfies the conditions, print −1.

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

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.
Editor Image

?