Submission #66386537


Source Code Expand

#include <bits/stdc++.h>
namespace suisen {
    template <class T> bool chmin(T& x, const T& y) { return y >= x ? false : (x = y, true); }
    template <class T> bool chmax(T& x, const T& y) { return y <= x ? false : (x = y, true); }
    template <class T> constexpr int pow_m1(T n) { return -(n & 1) | 1; }
    template <class T> constexpr T fld(const T x, const T y) { T q = x / y, r = x % y; return q - ((x ^ y) < 0 and (r != 0)); }
    template <class T> constexpr T cld(const T x, const T y) { T q = x / y, r = x % y; return q + ((x ^ y) > 0 and (r != 0)); }
}
namespace suisen::macro {
#define IMPL_REPITER(cond) auto& begin() { return *this; } auto end() { return nullptr; } auto& operator*() { return _val; } auto& operator++() { return _val += _step, *this; } bool operator!=(std::nullptr_t) { return cond; }
    template <class Int, class IntL = Int, class IntStep = Int, std::enable_if_t<(std::is_signed_v<Int> == std::is_signed_v<IntL>), std::nullptr_t> = nullptr> struct rep_impl {
        Int _val; const Int _end, _step;
        rep_impl(Int n) : rep_impl(0, n) {}
        rep_impl(IntL l, Int r, IntStep step = 1) : _val(l), _end(r), _step(step) {}
        IMPL_REPITER((_val < _end))
    };
    template <class Int, class IntL = Int, class IntStep = Int, std::enable_if_t<(std::is_signed_v<Int> == std::is_signed_v<IntL>), std::nullptr_t> = nullptr> struct rrep_impl {
        Int _val; const Int _end, _step;
        rrep_impl(Int n) : rrep_impl(0, n) {}
        rrep_impl(IntL l, Int r) : _val(r - 1), _end(l), _step(-1) {}
        rrep_impl(IntL l, Int r, IntStep step) : _val(l + fld<Int>(r - l - 1, step) * step), _end(l), _step(-step) {}
        IMPL_REPITER((_val >= _end))
    };
    template <class Int, class IntStep = Int> struct repinf_impl {
        Int _val; const Int _step;
        repinf_impl(Int l, IntStep step = 1) : _val(l), _step(step) {}
        IMPL_REPITER((true))
    };
#undef IMPL_REPITER
}

#include <iostream>

#include <limits>
#include <type_traits>

namespace suisen {
    template <typename ...Constraints> using constraints_t = std::enable_if_t<std::conjunction_v<Constraints...>, std::nullptr_t>;

    template <typename T, typename = std::nullptr_t> struct bitnum { static constexpr int value = 0; };
    template <typename T> struct bitnum<T, constraints_t<std::is_integral<T>>> { static constexpr int value = std::numeric_limits<std::make_unsigned_t<T>>::digits; };
    template <typename T> static constexpr int bitnum_v = bitnum<T>::value;
    template <typename T, size_t n> struct is_nbit { static constexpr bool value = bitnum_v<T> == n; };
    template <typename T, size_t n> static constexpr bool is_nbit_v = is_nbit<T, n>::value;

    template <typename T, typename = std::nullptr_t> struct safely_multipliable { using type = T; };
    template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 32>>> { using type = long long; };
    template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 64>>> { using type = __int128_t; };
    template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 32>>> { using type = unsigned long long; };
    template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 64>>> { using type = __uint128_t; };
    template <typename T> using safely_multipliable_t = typename safely_multipliable<T>::type;

    template <typename T, typename = void> struct rec_value_type { using type = T; };
    template <typename T> struct rec_value_type<T, std::void_t<typename T::value_type>> {
        using type = typename rec_value_type<typename T::value_type>::type;
    };
    template <typename T> using rec_value_type_t = typename rec_value_type<T>::type;

    template <typename T> class is_iterable {
        template <typename T_> static auto test(T_ e) -> decltype(e.begin(), e.end(), std::true_type{});
        static std::false_type test(...);
    public:
        static constexpr bool value = decltype(test(std::declval<T>()))::value;
    };
    template <typename T> static constexpr bool is_iterable_v = is_iterable<T>::value;
    template <typename T> class is_writable {
        template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::ostream&>() << e, std::true_type{});
        static std::false_type test(...);
    public:
        static constexpr bool value = decltype(test(std::declval<T>()))::value;
    };
    template <typename T> static constexpr bool is_writable_v = is_writable<T>::value;
    template <typename T> class is_readable {
        template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::istream&>() >> e, std::true_type{});
        static std::false_type test(...);
    public:
        static constexpr bool value = decltype(test(std::declval<T>()))::value;
    };
    template <typename T> static constexpr bool is_readable_v = is_readable<T>::value;
} // namespace suisen
namespace suisen::io {
    template <typename IStream, std::enable_if_t<std::conjunction_v<std::is_base_of<std::istream, std::remove_reference_t<IStream>>, std::negation<std::is_const<std::remove_reference_t<IStream>>>>, std::nullptr_t> = nullptr>
    struct InputStream {
    private:
        using istream_type = std::remove_reference_t<IStream>;
        IStream is;
        struct { InputStream* is; template <typename T> operator T() { T e; *is >> e; return e; } } _reader{ this };
    public:
        template <typename IStream_> InputStream(IStream_ &&is) : is(std::move(is)) {}
        template <typename IStream_> InputStream(IStream_ &is) : is(is) {}
        template <typename T> InputStream& operator>>(T& e) {
            if constexpr (suisen::is_readable_v<T>) is >> e; else _read(e);
            return *this;
        }
        auto read() { return _reader; }
        template <typename Head, typename... Tail>
        void read(Head& head, Tail &...tails) { ((*this >> head) >> ... >> tails); }
        istream_type& get_stream() { return is; }
    private:
        static __uint128_t _stou128(const std::string& s) {
            __uint128_t ret = 0;
            for (char c : s) if ('0' <= c and c <= '9') ret = 10 * ret + c - '0';
            return ret;
        }
        static __int128_t _stoi128(const std::string& s) { return (s[0] == '-' ? -1 : +1) * _stou128(s); }

        void _read(__uint128_t& v) { v = _stou128(std::string(_reader)); }
        void _read(__int128_t& v) { v = _stoi128(std::string(_reader)); }
        template <typename T, typename U>
        void _read(std::pair<T, U>& a) { *this >> a.first >> a.second; }
        template <size_t N = 0, typename ...Args>
        void _read(std::tuple<Args...>& a) { if constexpr (N < sizeof...(Args)) *this >> std::get<N>(a), _read<N + 1>(a); }
        template <typename Iterable, std::enable_if_t<suisen::is_iterable_v<Iterable>, std::nullptr_t> = nullptr>
        void _read(Iterable& a) { for (auto& e : a) *this >> e; }
    };
    template <typename IStream>
    InputStream(IStream &&) -> InputStream<IStream>;
    template <typename IStream>
    InputStream(IStream &) -> InputStream<IStream&>;

    InputStream cin{ std::cin };

    auto read() { return cin.read(); }
    template <typename Head, typename... Tail>
    void read(Head& head, Tail &...tails) { cin.read(head, tails...); }
} // namespace suisen::io
namespace suisen { using io::read; } // namespace suisen

namespace suisen::io {
    template <typename OStream, std::enable_if_t<std::conjunction_v<std::is_base_of<std::ostream, std::remove_reference_t<OStream>>, std::negation<std::is_const<std::remove_reference_t<OStream>>>>, std::nullptr_t> = nullptr>
    struct OutputStream {
    private:
        using ostream_type = std::remove_reference_t<OStream>;
        OStream os;
    public:
        template <typename OStream_> OutputStream(OStream_ &&os) : os(std::move(os)) {}
        template <typename OStream_> OutputStream(OStream_ &os) : os(os) {}
        template <typename T> OutputStream& operator<<(const T& e) {
            if constexpr (suisen::is_writable_v<T>) os << e; else _print(e);
            return *this;
        }
        void print() { *this << '\n'; }
        template <typename Head, typename... Tail>
        void print(const Head& head, const Tail &...tails) { *this << head, ((*this << ' ' << tails), ...), *this << '\n'; }
        template <typename Iterable, std::enable_if_t<suisen::is_iterable_v<Iterable>, std::nullptr_t> = nullptr>
        void print_all(const Iterable& v, std::string sep = " ", std::string end = "\n") {
            for (auto it = v.begin(); it != v.end();) if (*this << *it; ++it != v.end()) *this << sep;
            *this << end;
        }
        ostream_type& get_stream() { return os; }
    private:
        void _print(__uint128_t value) {
            char buffer[41], *d = std::end(buffer);
            do *--d = '0' + (value % 10), value /= 10; while (value);
            os.rdbuf()->sputn(d, std::end(buffer) - d);
        }
        void _print(__int128_t value) {
            if (value < 0) *this << '-';
            _print(__uint128_t(value < 0 ? -value : value));
        }
        template <typename T, typename U>
        void _print(const std::pair<T, U>& a) { *this << a.first << ' ' << a.second; }
        template <size_t N = 0, typename ...Args>
        void _print(const std::tuple<Args...>& a) {
            if constexpr (N < std::tuple_size_v<std::tuple<Args...>>) {
                if constexpr (N) *this << ' ';
                *this << std::get<N>(a), _print<N + 1>(a);
            }
        }
        template <typename Iterable, std::enable_if_t<suisen::is_iterable_v<Iterable>, std::nullptr_t> = nullptr>
        void _print(const Iterable& a) { print_all(a, " ", ""); }
    };
    template <typename OStream_>
    OutputStream(OStream_ &&) -> OutputStream<OStream_>;
    template <typename OStream_>
    OutputStream(OStream_ &) -> OutputStream<OStream_&>;

    OutputStream cout{ std::cout }, cerr{ std::cerr };

    template <typename... Args>
    void print(const Args &... args) { cout.print(args...); }
    template <typename Iterable, std::enable_if_t<suisen::is_iterable_v<Iterable>, std::nullptr_t> = nullptr>
    void print_all(const Iterable& v, const std::string& sep = " ", const std::string& end = "\n") { cout.print_all(v, sep, end); }
} // namespace suisen::io
namespace suisen { using io::print, io::print_all; } // namespace suisen

namespace suisen {
    template <class T, class ToKey, class CompKey = std::less<>, std::enable_if_t<std::conjunction_v<std::is_invocable<ToKey, T>, std::is_invocable_r<bool, CompKey, std::invoke_result_t<ToKey, T>, std::invoke_result_t<ToKey, T>>>, std::nullptr_t> = nullptr>
    auto comparator(const ToKey& to_key, const CompKey& comp_key = std::less<>()) {
        return [=](const T& x, const T& y) { return comp_key(to_key(x), to_key(y)); };
    }
    template <class Compare, std::enable_if_t<std::is_invocable_r_v<bool, Compare, int, int>, std::nullptr_t> = nullptr>
    std::vector<int> sorted_indices(int n, const Compare& compare) {
        std::vector<int> p(n);
        return std::iota(p.begin(), p.end(), 0), std::sort(p.begin(), p.end(), compare), p;
    }
    template <class ToKey, std::enable_if_t<std::is_invocable_v<ToKey, int>, std::nullptr_t> = nullptr>
    std::vector<int> sorted_indices(int n, const ToKey& to_key) { return sorted_indices(n, comparator<int>(to_key)); }
    template <class T, class Comparator>
    auto priority_queue_with_comparator(const Comparator& comparator) { return std::priority_queue<T, std::vector<T>, Comparator>{ comparator }; }
    template <class Iterable, std::enable_if_t<suisen::is_iterable_v<Iterable>, std::nullptr_t> = nullptr>
    void sort_unique_erase(Iterable& a) { std::sort(a.begin(), a.end()), a.erase(std::unique(a.begin(), a.end()), a.end()); }

    template <size_t D> struct Dim : std::array<int, D> {
        template <typename ...Ints> Dim(const Ints& ...ns) : std::array<int, D>::array{ static_cast<int>(ns)... } {}
    };
    template <typename ...Ints> Dim(const Ints& ...) -> Dim<sizeof...(Ints)>;
    template <class T, size_t D, size_t I = 0>
    auto ndvec(const Dim<D> &ns, const T& value = {}) {
        if constexpr (I + 1 < D) {
            return std::vector(ns[I], ndvec<T, D, I + 1>(ns, value));
        } else {
            return std::vector<T>(ns[I], value);
        }
    }
}
namespace suisen {
    using int128 = __int128_t;
    using uint128 = __uint128_t;
    template <class T> using min_priority_queue = std::priority_queue<T, std::vector<T>, std::greater<T>>;
    template <class T> using max_priority_queue = std::priority_queue<T, std::vector<T>, std::less<T>>;
}
namespace suisen { const std::string Yes = "Yes", No = "No", YES = "YES", NO = "NO"; }

#ifdef LOCAL
#  define debug(...) debug_impl(#__VA_ARGS__, __VA_ARGS__)
template <class H, class... Ts> void debug_impl(const char* s, const H& h, const Ts&... t) {
    suisen::io::cerr << "[\033[32mDEBUG\033[m] " << s << ": " << h, ((suisen::io::cerr << ", " << t), ..., (suisen::io::cerr << "\n"));
}
#else
#  define debug(...) void(0)
#endif
#define FOR(e, v) for (auto &&e : v)
#define CFOR(e, v) for (const auto &e : v)
#define REP(i, ...) CFOR(i, suisen::macro::rep_impl(__VA_ARGS__))
#define RREP(i, ...) CFOR(i, suisen::macro::rrep_impl(__VA_ARGS__))
#define REPINF(i, ...) CFOR(i, suisen::macro::repinf_impl(__VA_ARGS__))
#define LOOP(n) for ([[maybe_unused]] const auto& _ : suisen::macro::rep_impl(n))
#define ALL(iterable) std::begin(iterable), std::end(iterable)

using namespace suisen;
using namespace std;
struct io_setup {
    io_setup(int precision = 20) {
        std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(precision);
    }
} io_setup_{};

constexpr int iinf = std::numeric_limits<int>::max() / 2;
constexpr long long linf = std::numeric_limits<long long>::max() / 2;

void f(vector<vector<int>> a) {
    int n = a.size();

    set<vector<vector<int>>> ss;

    REP(s, 1 << n) {
        REP(t, 1 << n) {
            auto b = a;

            REP(i, n) if ((s >> i) & 1) {
                REP(j, n) {
                    b[i][j] ^= 1;
                }
            }
            REP(j, n) if ((t >> j) & 1) {
                REP(i, n) {
                    b[i][j] ^= 1;
                }
            }

            bool ok = true;
            REP(i, n) {
                int num = 0;
                REP(j, n) num += b[i][j];
                ok &= (4 * num < n);
                if (not ok) break;
            }
            // REP(j, n) {
            //     int num = 0;
            //     REP(i, n) num += b[i][j];
            //     ok &= (4 * num < n);
            //     if (not ok) break;
            // }

            if (ok) {
                ss.insert(b);
                print(ss.size());
            }
        }
    }
    print(ss.size());

    for (auto b : ss) {
        for (auto row : b) print_all(row, "");
        print(string(10, '-'));
    }
}

#include <atcoder/dsu>

int n;
vector<vector<int>> a;
vector<int> r, c;

template <size_t N = 1>
void solve() {
    if constexpr (N < 1024) {
        if (N < size_t(n)) {
            return solve<N + N>();
        }
    }

    using Bits = bitset<N>;

    vector<Bits> rs(n), cs(n);
    REP(i, n) REP(j, n) {
        rs[i][j] = a[i][j];
        cs[i][j] = a[j][i];
    }

    atcoder::dsu ruf(2 * n), cuf(2 * n);
    REP(i, n) {
        REP(j, i + 1, n) {
            {
                int num = (rs[i] ^ rs[j]).count();
                if (num * 2 <= n) {
                    ruf.merge(i, j);
                    ruf.merge(n + i, n + j);
                }
                if (num * 2 >= n) {
                    ruf.merge(n + i, j);
                    ruf.merge(i, n + j);
                }
                // debug("R", i, j, num);

                if (ruf.same(i, n + i)) {
                    print(No);
                    return;
                }
            }
            {
                int num = (cs[i] ^ cs[j]).count();
                if (num * 2 <= n) {
                    cuf.merge(i, j);
                    cuf.merge(n + i, n + j);
                }
                if (num * 2 >= n) {
                    cuf.merge(n + i, j);
                    cuf.merge(i, n + j);
                }
                // debug("C", i, j, num);

                if (cuf.same(i, n + i)) {
                    print(No);
                    return;
                }
            }
        }
    }
    REP(i, n) {
        if (ruf.same(i, n + i)) {
            print(No);
            return;
        }
        if (cuf.same(i, n + i)) {
            print(No);
            return;
        }
    }

    vector<int> x(n), y(n);

    REP(i, n) {
        x[i] = ruf.same(0, i);
        y[i] = cuf.same(0, i);
    }

    debug(x);
    debug(y);

    LOOP(2) {
        vector b(n, vector<int>(n));
        REP(i, n) REP(j, n) {
            b[i][j] = a[i][j] ^ x[i] ^ y[j];
        }

        // print(string(10, '-'));
        // REP(i, n) {
        //     print_all(b[i], "");
        // }

        bool ok = true;
        REP(i, n) {
            int nr = 0, nc = 0;
            REP(j, n) {
                nr += b[i][j];
                nc += b[j][i];
            }
            ok &= nr == r[i];
            ok &= nc == c[i];
            if (not ok) break;
        }
        if (ok) {
            print(Yes);
            print_all(x, "");
            print_all(y, "");
            return;
        }

        for (auto& e : y) e ^= 1;
    }
    print(No);

    // mt19937 rng{ random_device{}() };

    // int n;
    // read(n);
    // vector<vector<int>> a(n, vector<int>(n));
    // REP(i, n) REP(j, n) {
    //     a[i][j] = (i == j);
    // }
    // f(a);

    // return;
}

int main() {
    int test_case_num = 1;
    read(test_case_num);
    LOOP(test_case_num) {
        read(n);
        a.resize(n);
        REP(i, n) {
            a[i].resize(n);
            string s;
            read(s);
            REP(j, n) a[i][j] = s[j] - '0';
        }
        r.resize(n), c.resize(n);
        read(r, c);
        solve();
    }
    return 0;
}

Submission Info

Submission Time
Task A - Flip Row or Col 2
User suisen
Language C++ 20 (gcc 12.2)
Score 800
Code Size 18213 Byte
Status AC
Exec Time 84 ms
Memory 11364 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 1
AC × 39
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_test_00.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt, 01_test_27.txt, 01_test_28.txt, 01_test_29.txt, 01_test_30.txt, 01_test_31.txt, 01_test_32.txt, 01_test_33.txt, 01_test_34.txt, 01_test_35.txt, 01_test_36.txt, 01_test_37.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3388 KiB
01_test_00.txt AC 43 ms 3552 KiB
01_test_01.txt AC 3 ms 3588 KiB
01_test_02.txt AC 84 ms 3460 KiB
01_test_03.txt AC 73 ms 3504 KiB
01_test_04.txt AC 67 ms 3596 KiB
01_test_05.txt AC 59 ms 3452 KiB
01_test_06.txt AC 55 ms 3544 KiB
01_test_07.txt AC 52 ms 3484 KiB
01_test_08.txt AC 49 ms 3556 KiB
01_test_09.txt AC 47 ms 3536 KiB
01_test_10.txt AC 45 ms 3464 KiB
01_test_11.txt AC 44 ms 3456 KiB
01_test_12.txt AC 42 ms 3492 KiB
01_test_13.txt AC 40 ms 3552 KiB
01_test_14.txt AC 37 ms 3540 KiB
01_test_15.txt AC 35 ms 3400 KiB
01_test_16.txt AC 33 ms 3612 KiB
01_test_17.txt AC 32 ms 3560 KiB
01_test_18.txt AC 31 ms 3524 KiB
01_test_19.txt AC 31 ms 3424 KiB
01_test_20.txt AC 29 ms 3544 KiB
01_test_21.txt AC 28 ms 3616 KiB
01_test_22.txt AC 29 ms 3556 KiB
01_test_23.txt AC 27 ms 3668 KiB
01_test_24.txt AC 27 ms 3824 KiB
01_test_25.txt AC 29 ms 4032 KiB
01_test_26.txt AC 31 ms 4444 KiB
01_test_27.txt AC 29 ms 5244 KiB
01_test_28.txt AC 34 ms 5296 KiB
01_test_29.txt AC 14 ms 7516 KiB
01_test_30.txt AC 14 ms 7624 KiB
01_test_31.txt AC 45 ms 11364 KiB
01_test_32.txt AC 44 ms 11300 KiB
01_test_33.txt AC 43 ms 11308 KiB
01_test_34.txt AC 40 ms 3588 KiB
01_test_35.txt AC 30 ms 3508 KiB
01_test_36.txt AC 25 ms 3612 KiB
01_test_37.txt AC 27 ms 3764 KiB