結果

問題 No.3736 Purely Bool Hell
コンテスト
ユーザー noya2
提出日時 2026-09-19 16:22:01
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1,364 ms / 3,000 ms
+ 218µs
コード長 21,759 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,929 ms
コンパイル使用メモリ 361,112 KB
実行使用メモリ 14,520 KB
最終ジャッジ日時 2026-09-19 16:22:19
合計ジャッジ時間 17,605 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge5_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#line 2 "/Users/noya2/Desktop/Noya2_library/template/template.hpp"
using namespace std;

#include<bits/stdc++.h>
#line 1 "/Users/noya2/Desktop/Noya2_library/template/inout_old.hpp"
namespace noya2 {

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p){
    os << p.first << " " << p.second;
    return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p){
    is >> p.first >> p.second;
    return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v){
    int s = (int)v.size();
    for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i];
    return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v){
    for (auto &x : v) is >> x;
    return is;
}

void in() {}
template <typename T, class... U>
void in(T &t, U &...u){
    cin >> t;
    in(u...);
}

void out() { cout << "\n"; }
template <typename T, class... U, char sep = ' '>
void out(const T &t, const U &...u){
    cout << t;
    if (sizeof...(u)) cout << sep;
    out(u...);
}

template<typename T>
void out(const vector<vector<T>> &vv){
    int s = (int)vv.size();
    for (int i = 0; i < s; i++) out(vv[i]);
}

struct IoSetup {
    IoSetup(){
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
        cerr << fixed << setprecision(7);
    }
} iosetup_noya2;

} // namespace noya2
#line 1 "/Users/noya2/Desktop/Noya2_library/template/const.hpp"
namespace noya2{

const int iinf = 1'000'000'007;
const long long linf = 2'000'000'000'000'000'000LL;
const long long mod998 =  998244353;
const long long mod107 = 1000000007;
const long double pi = 3.14159265358979323;
const vector<int> dx = {0,1,0,-1,1,1,-1,-1};
const vector<int> dy = {1,0,-1,0,1,-1,-1,1};
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string NUM = "0123456789";

void yes(){ cout << "Yes\n"; }
void no(){ cout << "No\n"; }
void YES(){ cout << "YES\n"; }
void NO(){ cout << "NO\n"; }
void yn(bool t){ t ? yes() : no(); }
void YN(bool t){ t ? YES() : NO(); }

} // namespace noya2
#line 2 "/Users/noya2/Desktop/Noya2_library/template/utils.hpp"

#line 6 "/Users/noya2/Desktop/Noya2_library/template/utils.hpp"

namespace noya2{

unsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){
    if (a == 0 || b == 0) return a + b;
    int n = __builtin_ctzll(a); a >>= n;
    int m = __builtin_ctzll(b); b >>= m;
    while (a != b) {
        int mm = __builtin_ctzll(a - b);
        bool f = a > b;
        unsigned long long c = f ? a : b;
        b = f ? b : a;
        a = (c - b) >> mm;
    }
    return a << std::min(n, m);
}

template<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(std::abs(a),std::abs(b))); }

long long sqrt_fast(long long n) {
    if (n <= 0) return 0;
    long long x = sqrt(n);
    while ((x + 1) * (x + 1) <= n) x++;
    while (x * x > n) x--;
    return x;
}

template<typename T> T floor_div(const T n, const T d) {
    assert(d != 0);
    return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);
}

template<typename T> T ceil_div(const T n, const T d) {
    assert(d != 0);
    return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);
}

template<typename T> void uniq(std::vector<T> &v){
    std::sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}

template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }

template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }

template<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }

} // namespace noya2
#line 8 "/Users/noya2/Desktop/Noya2_library/template/template.hpp"

#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)
#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)
#define all(v) (v).begin(),(v).end()

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;

namespace noya2{

/* ~ (. _________ . /) */

}

using namespace noya2;


#line 2 "c.cpp"

#line 2 "/Users/noya2/Desktop/Noya2_library/flow/maxflow.hpp"

#line 2 "/Users/noya2/Desktop/Noya2_library/data_structure/simple_queue.hpp"

#line 4 "/Users/noya2/Desktop/Noya2_library/data_structure/simple_queue.hpp"

namespace noya2::internal {

template <class T>
struct simple_queue {
    std::vector<T> payload;
    int pos = 0;
    void reserve(int n) { payload.reserve(n); }
    int size() const { return int(payload.size()) - pos; }
    bool empty() const { return pos == int(payload.size()); }
    void push(const T& t) { payload.push_back(t); }
    void emplace(auto ...vs) { payload.emplace_back(vs...); }
    T& front() { return payload[pos]; }
    void clear() {
        payload.clear();
        pos = 0;
    }
    void pop() { pos++; }
};

} // namespace noya2::internal
#line 4 "/Users/noya2/Desktop/Noya2_library/flow/maxflow.hpp"

#line 10 "/Users/noya2/Desktop/Noya2_library/flow/maxflow.hpp"

namespace noya2 {

template <class Cap> struct mf_graph {
  public:
    mf_graph() : _n(0) {}
    explicit mf_graph(int n) : _n(n), g(n) {}

    int add_edge(int from, int to, Cap cap) {
        assert(0 <= from && from < _n);
        assert(0 <= to && to < _n);
        assert(0 <= cap);
        int m = int(pos.size());
        pos.push_back({from, int(g[from].size())});
        int from_id = int(g[from].size());
        int to_id = int(g[to].size());
        if (from == to) to_id++;
        g[from].push_back(_edge{to, to_id, cap});
        g[to].push_back(_edge{from, from_id, 0});
        return m;
    }

    struct edge {
        int from, to;
        Cap cap, flow;
    };

    edge get_edge(int i) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        auto _e = g[pos[i].first][pos[i].second];
        auto _re = g[_e.to][_e.rev];
        return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
    }
    std::vector<edge> edges() {
        int m = int(pos.size());
        std::vector<edge> result;
        for (int i = 0; i < m; i++) {
            result.push_back(get_edge(i));
        }
        return result;
    }
    void change_edge(int i, Cap new_cap, Cap new_flow) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        assert(0 <= new_flow && new_flow <= new_cap);
        auto& _e = g[pos[i].first][pos[i].second];
        auto& _re = g[_e.to][_e.rev];
        _e.cap = new_cap - new_flow;
        _re.cap = new_flow;
    }

    Cap flow(int s, int t) {
        return flow(s, t, std::numeric_limits<Cap>::max());
    }
    Cap flow(int s, int t, Cap flow_limit) {
        assert(0 <= s && s < _n);
        assert(0 <= t && t < _n);
        assert(s != t);

        std::vector<int> level(_n), iter(_n);
        internal::simple_queue<int> que;

        auto bfs = [&]() {
            std::fill(level.begin(), level.end(), -1);
            level[s] = 0;
            que.clear();
            que.push(s);
            while (!que.empty()) {
                int v = que.front();
                que.pop();
                for (auto e : g[v]) {
                    if (e.cap == 0 || level[e.to] >= 0) continue;
                    level[e.to] = level[v] + 1;
                    if (e.to == t) return;
                    que.push(e.to);
                }
            }
        };
        auto dfs = [&](auto self, int v, Cap up) {
            if (v == s) return up;
            Cap res = 0;
            int level_v = level[v];
            for (int& i = iter[v]; i < int(g[v].size()); i++) {
                _edge& e = g[v][i];
                if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;
                Cap d =
                    self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));
                if (d <= 0) continue;
                g[v][i].cap += d;
                g[e.to][e.rev].cap -= d;
                res += d;
                if (res == up) return res;
            }
            level[v] = _n;
            return res;
        };

        Cap flow = 0;
        while (flow < flow_limit) {
            bfs();
            if (level[t] == -1) break;
            std::fill(iter.begin(), iter.end(), 0);
            Cap f = dfs(dfs, t, flow_limit - flow);
            if (!f) break;
            flow += f;
        }
        return flow;
    }

    std::vector<bool> min_cut(int s) {
        std::vector<bool> visited(_n);
        internal::simple_queue<int> que;
        que.push(s);
        while (!que.empty()) {
            int p = que.front();
            que.pop();
            visited[p] = true;
            for (auto e : g[p]) {
                if (e.cap && !visited[e.to]) {
                    visited[e.to] = true;
                    que.push(e.to);
                }
            }
        }
        return visited;
    }

  private:
    int _n;
    struct _edge {
        int to, rev;
        Cap cap;
    };
    std::vector<std::pair<int, int>> pos;
    std::vector<std::vector<_edge>> g;
};

} // namespace noya2

#line 2 "/Users/noya2/Desktop/Noya2_library/data_structure/csr.hpp"

#line 4 "/Users/noya2/Desktop/Noya2_library/data_structure/csr.hpp"
#include<ranges>
#line 7 "/Users/noya2/Desktop/Noya2_library/data_structure/csr.hpp"

namespace noya2::internal {

template<class E>
struct csr {
    csr () {}
    csr (int _n) : n(_n) {}
    csr (int _n, int m) : n(_n){
        start.reserve(m);
        elist.reserve(m);
    }
    // ACL style constructor (do not have to call build)
    csr (int _n, const std::vector<std::pair<int,E>> &idx_elem) : n(_n), start(_n + 2), elist(idx_elem.size()) {
        for (auto &[i, e] : idx_elem){
            start[i + 2]++;
        }
        for (int i = 1; i < n; i++){
            start[i + 2] += start[i + 1];
        }
        for (auto &[i, e] : idx_elem){
            elist[start[i + 1]++] = e;
        }
        prepared = true;
    }
    int add(int idx, E elem){
        int eid = start.size();
        start.emplace_back(idx);
        elist.emplace_back(elem);
        return eid;
    }
    void build(){
        if (prepared) return ;
        int m = start.size();
        std::vector<E> nelist(m);
        std::vector<int> nstart(n + 2, 0);
        for (int i = 0; i < m; i++){
            nstart[start[i] + 2]++;
        }
        for (int i = 1; i < n; i++){
            nstart[i + 2] += nstart[i + 1];
        }
        for (int i = 0; i < m; i++){
            nelist[nstart[start[i] + 1]++] = elist[i];
        }
        swap(elist,nelist);
        swap(start,nstart);
        prepared = true;
    }
    const auto operator[](int idx) const {
        return std::ranges::subrange(elist.begin()+start[idx],elist.begin()+start[idx+1]);
    }
    auto operator[](int idx){
        return std::ranges::subrange(elist.begin()+start[idx],elist.begin()+start[idx+1]);
    }
    const auto operator()(int idx, int l, int r) const {
        return std::ranges::subrange(elist.begin()+start[idx]+l,elist.begin()+start[idx]+r);
    }
    auto operator()(int idx, int l, int r){
        return std::ranges::subrange(elist.begin()+start[idx]+l,elist.begin()+start[idx]+r);
    }
    size_t size() const {
        return n;
    }
    int n;
    std::vector<int> start;
    std::vector<E> elist;
    bool prepared = false;
};

} // namespace noya2::internal
#line 151 "/Users/noya2/Desktop/Noya2_library/flow/maxflow.hpp"

namespace noya2 {

template<class Cap>
struct maxflow {
    struct edge {
        int from, to;
        Cap cap, flow;
    };
    maxflow() : n(0) {}
    explicit maxflow(int _n) : n(_n), g(n), degree(_n,0) {}

    int add_edge(int from, int to, Cap cap){
        assert(0 <= from && from < n);
        assert(0 <= to && to < n);
        assert(0 <= cap);

        int eid = (int)(pos.size());
        int from_id = degree[from];
        int to_id = degree[to] + (from == to ? 1 : 0);

        g.add(from, _edge{to, to_id, cap});
        degree[from]++;
        g.add(to, _edge{from, from_id, 0});
        degree[to]++;
        pos.emplace_back(from, from_id);

        return eid;
    }

    edge get_edge(int i) {
        auto e = g[pos[i].first][pos[i].second];
        auto re = g[e.to][e.rev];
        return edge{pos[i].first, e.to, e.cap + re.cap, re.cap};
    }

    Cap flow(int s, int t){
        return flow(s, t, std::numeric_limits<Cap>::max());
    }

    Cap flow(int s, int t, Cap flow_limit){
        assert(0 <= s && s < n);
        assert(0 <= t && t < n);
        assert(s != t);
        g.build();

        std::vector<int> level(n), iter(n);
        internal::simple_queue<int> que;

        auto bfs = [&]() {
            std::fill(level.begin(), level.end(), -1);
            level[s] = 0;
            que.clear();
            que.push(s);
            while (!que.empty()){
                int v = que.front(); que.pop();
                for (auto e : g[v]){
                    if (e.cap == 0 || level[e.to] >= 0) continue;
                    level[e.to] = level[v] + 1;
                    if (e.to == t) return ;
                    que.push(e.to);
                }
            }
        };
        auto dfs = [&](auto sfs, int v, Cap up){
            if (v == s) return up;
            Cap res = 0;
            int level_v = level[v];
            for (int &i = iter[v]; i < degree[v]; i++){
                _edge &e = g[v][i];
                if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;
                Cap d = sfs(sfs, e.to, std::min(up - res, g[e.to][e.rev].cap));
                if (d <= 0) continue;
                g[v][i].cap += d;
                g[e.to][e.rev].cap -= d;
                res += d;
                if (res == up) return res;
            }
            level[v] = n;
            return res;
        };

        Cap flow = 0;
        while (flow < flow_limit){
            bfs();
            if (level[t] == -1) break;
            std::fill(iter.begin(), iter.end(), 0);
            Cap f = dfs(dfs, t, flow_limit - flow);
            if (!f) break;
            flow += f;
        }
        return flow;
    }

    std::vector<bool> min_cut(int s){
        std::vector<bool> visited(n);
        internal::simple_queue<int> que;
        que.push(s);
        visited[s] = true;
        while (!que.empty()){
            int v = que.front(); que.pop();
            for (auto e : g[v]){
                if (e.cap && !visited[e.to]){
                    visited[e.to] = true;
                    que.push(e.to);
                }
            }
        }
        return visited;
    }

//   private:
    int n;
    struct _edge {
        int to, rev;
        Cap cap;
    };
    std::vector<std::pair<int, int>> pos;
    internal::csr<_edge> g;
    std::vector<int> degree;
};

} // namespace noya2
#line 4 "c.cpp"

vector<vector<int>> make2(int n, vector<int> b, vector<int> c){
    assert(ranges::min(b) == 0);
    vector g(n,vector<int>(n,-1));
    rep(j,n){
        if (b[j] == 0){
            rep(i,n){
                g[i][j] = 0;
            }
        }
    }
    vector<int> needps;
    rep(p,n*2-1){
        int blank = 0;
        rep(i,n){
            int j = p-i;
            if (0 <= j && j < n){
                if (g[i][j] == -1){
                    blank++;
                }
            }
        }
        if (blank % 2 == c[p]){
            rep(i,n){
                int j = p-i;
                if (0 <= j && j < n){
                    if (g[i][j] == -1){
                        g[i][j] = 1;
                    }
                }
            }
        }
        else {
            needps.emplace_back(p);
        }
    }
    vector<bool> isneedjs(n);
    rep(j,n){
        if (b[j] == 0) continue;
        int one = 0;
        rep(i,n){
            if (g[i][j] == 1){
                one++;
            }
        }
        if (one >= 1) continue;
        isneedjs[j] = 1;
    }
    int lv = n*2-1, rv = n;
    int s = lv+rv, t = s+1;
    mf_graph<int> fl(t+1);
    for (int p : needps){
        fl.add_edge(s,p,1);
        rep(i,n){
            int j = p-i;
            if (0 <= j && j < n){
                if (g[i][j] != -1) continue;
                fl.add_edge(p,lv+j,1);
            }
        }
    }
    rep(j,n){
        if (isneedjs[j]){
            fl.add_edge(lv+j,t,n-1);
        }
        else {
            fl.add_edge(lv+j,t,n);
        }
    }
    auto mf = fl.flow(s,t);
    if (mf != ssize(needps)){
        return {};
    }
    auto es = fl.edges();
    for (auto e : es){
        if (e.flow == 0) continue;
        int p = e.from;
        int j = e.to - lv;
        if (0 <= p && p < lv && 0 <= j && j < n){
            int i = p-j;
            g[i][j] = 0;
        }
    }
    rep(i,n) rep(j,n){
        if (g[i][j] == -1){
            g[i][j] = 1;
        }
    }
    return g;
}

vector<vector<int>> make(int n, vector<int> a, vector<int> b, vector<int> c){
    vector g(n,vector<int>(n,-1));
    rep(i,n){
        if (a[i] == 1){
            rep(j,n){
                g[i][j] = 1;
            }
        }
    }
    rep(j,n){
        if (b[j] == 0){
            rep(i,n){
                if (g[i][j] == 1){
                    return {};
                }
                g[i][j] = 0;
            }
        }
    }
    auto tekitou = [&](){
        rep(p,n*2-1){
            vector<int> ids;
            int cur = 0;
            rep(i,n){
                int j = p-i;
                if (0 <= j && j < n){
                    if (g[i][j] == -1){
                        ids.emplace_back(i);
                    }
                    else {
                        cur ^= g[i][j];
                    }
                }
            }
            if (!ids.empty()){
                int i = ids.back(); ids.pop_back();
                int j = p-i;
                g[i][j] = cur ^ c[p];
                cur = c[p];
            }
            if (cur != c[p]){
                assert(ids.empty());
                return ;
            }
            for (int i : ids){
                int j = p-i;
                g[i][j] = 0;
            }
        }
    };
    if ([&]{
        rep(j,n){
            if (b[j] == 0) return true;
        }
        return false;
    }()){
        return make2(n,b,c);
    }
    if ([&]{
        rep(i,n){
            if (a[i] == 1) return true;
        }
        return false;
    }()){
        rep(i,n){
            a[i] ^= 1;
        }
        rep(p,n*2-1){
            int cnt = 0;
            rep(i,n){
                int j = p-i;
                if (0 <= j && j < n){
                    cnt ^= 1;
                }
            }
            c[p] ^= cnt;
        }
        g = make2(n,a,c);
        if (g.empty()){
            return {};
        }
        rep(i,n) rep(j,n){
            g[i][j] ^= 1;
        }
        rep(i,n) rep(j,n){
            if (i < j){
                swap(g[i][j],g[j][i]);
            }
        }
        return g;
    }
    g[0][0] = c[0];
    g[n-1][n-1] = c[n*2-2];
    if (n == 1) return g;
    if (n == 2){
        if (g[0][0] != g[1][1]){
            return {};
        }
        g[0][1] = g[1][0] = (1 ^ g[0][0]);
        return g;
    }
    if (g[0][0] == g[n-1][n-1]){
        repp(i,1,n){
            g[i][i] = g[0][0];
        }
        int pari = g[0][0]^1;
        rep(i,n){
            int j = (i+1)%n;
            g[i][j] = pari;
        }
        tekitou();
        return g;
    }
    else {
        int x = g[0][0], y = g[n-1][n-1];
        rep(j,n){
            g[0][j] = x;
        }
        rep(i,n){
            g[i][n-1] = y;
        }
        g[0][n-1] = x;
        g[0][n-2] = y;
        g[1][n-2] = x;
        if (g[0][0] == 0){
            rep(i,n) rep(j,n){
                if (i < j){
                    swap(g[i][j],g[j][i]);
                }
            }
        }
        tekitou();
        return g;
    }
}

vector<vector<int>> solve1(int n, vector<int> a, vector<int> b, vector<int> c){
    vector ans(n,vector<int>(n));
    rep(d,30){
        vector<int> na(n), nb(n), nc(n*2-1);
        rep(i,n){
            na[i] = (a[i] >> d & 1);
            nb[i] = (b[i] >> d & 1);
        }
        rep(i,n*2-1){
            nc[i] = (c[i] >> d & 1);
        }
        auto bits = make(n,na,nb,nc);
        // if (d == 2){
        //     out(na);
        //     out(nb);
        //     out(nc);
        //     out(":");
        //     out(bits);
        // }
        if (bits.empty()){
            return {};
        }
        rep(i,n) rep(j,n){
            if (bits[i][j] == -1){
                return {};
            }
        }
        rep(i,n) rep(j,n){
            ans[i][j] |= (bits[i][j] << d);
        }
    }
    rep(i,n){
        int x = (1<<30)-1;
        rep(j,n){
            x &= ans[i][j];
        }
        if (x != a[i]){
            // out("xneq");
            return {};
        }
    }
    rep(j,n){
        int x = 0;
        rep(i,n){
            x |= ans[i][j];
        }
        if (x != b[j]){
            // out("bneq",x,b[j]);
            return {};
        }
    }
    rep(p,n*2-1){
        int x = 0;
        rep(i,n){
            int j = p-i;
            if (0 <= j && j < n){
                x ^= ans[i][j];
            }
        }
        if (x != c[p]){
            // out("cneq");
            return {};
        }
    }
    return ans;
}

void solve(){
    int n; in(n);
    vector<int> a(n), b(n), c(n*2-1); in(a,b,c);
    auto ans = solve1(n,a,b,c);
    if (ans.empty()){
        out(-1);
        // exit(1);
        return ;
    }
    out(ans);
}

int main(){
    int t = 1; in(t);
    while (t--) { solve(); }
}
0