Language: C++

#include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <assert.h>
using namespace std;


#define REP(i, n) for(ll i = 0;i < n;i++)
#define REPR(i, n) for(ll i = n;i >= 0;i--)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define FORR(i, m, n) for(ll i = m;i >= n;i--)
#define REPO(i, n) for(ll i = 1;i <= n;i++)
#define ll unsigned long long
#define INF (ll)1 << 60
#define MINF (2 * INF)
#define ALL(n) n.begin(),n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll n, ans[510][510], ia[510], ib[510];
bool at[510], bt[510], ok = true;
int main() {
	cin >> n;
	REP(i, n)cin >> at[i];
	REP(i, n)cin >> bt[i];
	REP(i, n)cin >> ia[i];
	REP(i, n)cin >> ib[i];
	if (n == 1) {
		if (ia[0] == ib[0])cout << ia[0] << endl;
		else cout << -1 << endl;
		return 0;
	}
	REP(k, 64) {
		ll a[510] = {}, b[510] = {};
		vector<vector<ll>> s(510, vector<ll>(510, 2));
		bool a0 = false, a1 = false, b0 = false, b1 = false;
		ll ca0 = 0, ca1 = 0, cb0 = 0, cb1 = 0;
		REP(i, n) a[i] = ia[i] % 2;
		REP(i, n) b[i] = ib[i] % 2;
		REP(i, n) ia[i] /= 2;
		REP(i, n) ib[i] /= 2;
		// 0だけor1だけを置く
		REP(i, n) {
			if (at[i] and !a[i]) {
				REP(j, n) s[i][j] = 0;
				a0 = true;
				ca0++;
			}
			if (!at[i] and a[i]) {
				REP(j, n) s[i][j] = 1;
				a1 = true;
				ca1++;
			}
		}
		REP(i, n) {
			if (bt[i] and !b[i]) {
				REP(j, n) {
					if (s[j][i] == 1) ok = false;
					s[j][i] = 0;
					b0 = true;
					cb0++;
				}
			}
			if (!bt[i] and b[i]) {
				REP(j, n) {
					if (s[j][i] == 0) ok = false;
					s[j][i] = 1;
					b1 = true;
					cb1++;
				}
			}
		}
		if (!ok) {
			cout << -1 << endl;
			return 0;
		}
		//交差
		if (a0 and b0) {
			REP(i, n)REP(j, n)if (s[i][j] == 2)s[i][j] = 1;
		}
		else if (a1 and b1) {
			REP(i, n)REP(j, n)if (s[i][j] == 2)s[i][j] = 0;
		}
		//0かつ1
		else if (a0 and a1) {
			REP(i, n) {
				REP(j, n) {
					if (s[i][j] == 2) {
						if (j == 0)s[j][i] = 1;
						else s[j][i] = 0;
					}
				}
			}7
		}
		else if (b0 and b1) {
			REP(i, n) {
				REP(j, n) {
					if (s[i][j] == 2) {
						if (j == 0)s[i][j] = 1;
						else s[i][j] = 0;
					}
				}
			}
		}
		//0だけ or 1だけ(やばい)
		else if (a0) {//
			//猶予2以上
			if (ca0 <= n - 2) {//
				ll cnt = 0;
				REP(i, n) {
					if (s[i][0] == 2)cnt++;//
					REP(j, n) {
						if (s[i][j] == 2) {//
							if (cnt % 2 == j % 2)s[i][j] = 1;//
							else s[i][j] = 0;//
						}
					}
					
				}
			}
			//猶予1
			else {
				REP(i, n) {
					REP(j, n) {
						if (s[i][j] == 2) {//
							if (bt[j] and b[j])s[i][j] = 1;//
							else s[i][j] = 0;//
						}
					}
				}
			}
		}
		else if (a1) {//
			//猶予2以上
			if (ca1 <= n - 2) {//
				ll cnt = 0;
				REP(i, n) {
					if (s[i][0] == 2)cnt++;//
					REP(j, n) {
						if (s[i][j] == 2) {//
							if (cnt % 2 == j % 2)s[i][j] = 1;//
							else s[i][j] = 0;//
						}
					}

				}
			}
			//猶予1
			else {
				REP(i, n) {
					REP(j, n) {
						if (s[i][j] == 2) {//
							if (!bt[j] and !b[j])s[i][j] = 0;//
							else s[i][j] = 1;//
						}
					}
				}
			}
		}

		else if (b0) {//
			//猶予2以上
			if (cb0 <= n - 2) {//
				ll cnt = 0;
				REP(i, n) {
					if (s[0][i] == 2)cnt++;//
					REP(j, n) {
						if (s[j][i] == 2) {//
							if (cnt % 2 == j % 2)s[j][i] = 1;//
							else s[j][i] = 0;//
						}
					}

				}
			}
			//猶予1
			else {
				REP(i, n) {
					REP(j, n) {
						if (s[j][i] == 2) {//
							if (at[j] and a[j])s[j][i] = 1;//
							else s[j][i] = 0;//
						}
					}
				}
			}
		}
		else if (b1) {//
			//猶予2以上
			if (cb1 <= n - 2) {//
				ll cnt = 0;
				REP(i, n) {
					if (s[0][i] == 2)cnt++;//
					REP(j, n) {
						if (s[j][i] == 2) {//
							if (cnt % 2 == j % 2)s[j][i] = 1;//
							else s[j][i] = 0;//
						}
					}

				}
			}
			//猶予1
			else {
				REP(i, n) {
					REP(j, n) {
						if (s[j][i] == 2) {//
							if (!at[j] and !a[j])s[j][i] = 0;//
							else s[j][i] = 1;//
						}
					}
				}
			}
		}
		//置かれてない
		else {
			REP(i, n) {
				REP(j, n) {
					if (s[i][j] == 2) {
						if ((i + j) % 2 == 0)s[i][j] = 1;
						else s[i][j] = 0;
					}
				}
			}
		}
		// check
		REP(i, n) {//行
			if (at[i] and a[i]) {
				bool now = false;
				REP(j, n)if (s[i][j] == 1)now = true;
				if (!now) ok = false;
			}
			if (!at[i] and !a[i]) {
				bool now = false;
				REP(j, n)if (s[i][j] == 0)now = true;
				if (!now) ok = false;
			}
		}
		REP(i, n) {//列
			if (bt[i] and b[i]) {
				bool now = false;
				REP(j, n)if (s[j][i] == 1)now = true;
				if (!now) ok = false;
			}
			if (!bt[i] and !b[i]) {
				bool now = false;
				REP(j, n)if (s[j][i] == 0)now = true;
				if (!now) ok = false;
			}
		}
		if (!ok) {
			cout << -1 << endl;
			return 0;
		}
		//ansに足す
		REP(i, n) {
			REP(j, n) {
				ans[i][j] += ((ll)1 << k) * s[i][j];
			}
		}
		/*
		REP(i, n) {
			REP(j, n) {
				if (j != 0)cout << " ";
				cout << s[i][j];
			}
			cout << endl;
		}
		*/
	}
	REP(i, n) {
		REP(j, n) {
			if (j != 0)cout << " ";
				cout << ans[i][j];
		}
		cout << endl;
	}
}


Compiler output:
a.cc: In function 'int main()':
a.cc:105:27: error: expected ';' before '}' token
  105 |                         }7
      |                           ^
      |                           ;
  106 |                 }
      |                 ~