結果

問題 No.184 たのしい排他的論理和(HARD)
ユーザー KowerKoint2010KowerKoint2010
提出日時 2022-04-24 16:34:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 425 ms / 5,000 ms
コード長 11,144 bytes
コンパイル時間 2,461 ms
コンパイル使用メモリ 212,608 KB
実行使用メモリ 75,500 KB
最終ジャッジ日時 2023-09-08 10:06:36
合計ジャッジ時間 11,902 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 296 ms
54,324 KB
testcase_09 AC 61 ms
13,644 KB
testcase_10 AC 224 ms
42,004 KB
testcase_11 AC 161 ms
30,832 KB
testcase_12 AC 345 ms
62,760 KB
testcase_13 AC 383 ms
67,904 KB
testcase_14 AC 217 ms
40,136 KB
testcase_15 AC 405 ms
72,920 KB
testcase_16 AC 334 ms
61,236 KB
testcase_17 AC 373 ms
65,832 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 104 ms
75,320 KB
testcase_21 AC 425 ms
75,500 KB
testcase_22 AC 417 ms
75,268 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 247 ms
46,372 KB
testcase_29 AC 350 ms
64,836 KB
testcase_30 AC 307 ms
57,452 KB
testcase_31 AC 258 ms
50,296 KB
testcase_32 AC 323 ms
61,908 KB
testcase_33 AC 406 ms
73,180 KB
testcase_34 AC 402 ms
72,600 KB
testcase_35 AC 413 ms
74,852 KB
testcase_36 AC 414 ms
74,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "library/KowerKoint/base.hpp"

#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++)
#define ALL(a) (a).begin(),(a).end()
#define END(...) { print(__VA_ARGS__); return; }

using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using ll = long long;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
using VD = vector<double>;
using VVD = vector<VD>;
using VVVD = vector<VVD>;
using VS = vector<string>;
using VVS = vector<VS>;
using VVVS = vector<VVS>;
using VC = vector<char>;
using VVC = vector<VC>;
using VVVC = vector<VVC>;
using P = pair<int, int>;
using VP = vector<P>;
using VVP = vector<VP>;
using VVVP = vector<VVP>;
using LP = pair<ll, ll>;
using VLP = vector<LP>;
using VVLP = vector<VLP>;
using VVVLP = vector<VVLP>;

template <typename T>
using PQ = priority_queue<T>;
template <typename T>
using GPQ = priority_queue<T, vector<T>, greater<T>>;

constexpr int INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr int DX[] = {1, 0, -1, 0};
constexpr int DY[] = {0, 1, 0, -1};

void print() { cout << '\n'; }
template<typename T>
void print(const T &t) { cout << t << '\n'; }
template<typename Head, typename... Tail>
void print(const Head &head, const Tail &... tail) {
    cout << head << ' ';
    print(tail...);
}

#ifdef ONLINE_JUDGE
template<typename... Args>
void dbg(const Args &... args) {}
#else
void dbg() { cerr << '\n'; }
template<typename T>
void dbg(const T &t) { cerr << t << '\n'; }
template<typename Head, typename... Tail>
void dbg(const Head &head, const Tail &... tail) {
    cerr << head << ' ';
    dbg(tail...);
}
#endif

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
    os << p.first << " " << p.second;
    return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
    is >> p.first >> p.second;
    return is;
}

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

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
    for(T &in : v) is >> in;
    return is;
}

template<typename T>
vector<vector<T>> split(typename vector<T>::const_iterator begin, typename vector<T>::const_iterator end, T val) {
    vector<vector<T>> res;
    vector<T> cur;
    for(auto it = begin; it != end; it++) {
        if(*it == val) {
            res.push_back(cur);
            cur.clear();
        } else cur.push_back(val);
    }
    res.push_back(cur);
    return res;
}

vector<string> split(typename string::const_iterator begin, typename string::const_iterator end, char val) {
    vector<string> res;
    string cur = "";
    for(auto it = begin; it != end; it++) {
        if(*it == val) {
            res.push_back(cur);
            cur.clear();
        } else cur.push_back(val);
    }
    res.push_back(cur);
    return res;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template <typename T>
pair<VI, vector<T>> compress(const vector<T> &a) {
    int n = a.size();
    vector<T> x;
    REP(i, n) x.push_back(a[i]);
    sort(ALL(x)); x.erase(unique(ALL(x)), x.end());
    VI res(n);
    REP(i, n) res[i] = lower_bound(ALL(x), a[i]) - x.begin();
    return make_pair(res, x);
}

template <typename T>
pair<vector<T>, vector<T>> factorial(int n) {
    vector<T> res(n+1), rev(n+1);
    res[0] = 1;
    REP(i, n) res[i+1] = res[i] * (i+1);
    rev[n] = 1 / res[n];
    for(int i = n; i > 0; i--) {
        rev[i-1] = rev[i] * i;
    }
    return make_pair(res, rev);
}
#line 1 "library/KowerKoint/internal_operator.hpp"
namespace internal_operator {
    template <typename T>
    T default_add(T a, T b) { return a + b; }
    template <typename T>
    T default_sub(T a, T b) { return a - b; }
    template <typename T>
    T zero() { return T(0); }
    template <typename T>
    T default_div(T a, T b) { return a / b; }
    template <typename T>
    T default_mult(T a, T b) { return a * b; }
    template <typename T>
    T one() { return T(1); }
    template <typename T>
    T default_xor(T a, T b) { return a ^ b; }
    template <typename T>
    T default_and(T a, T b) { return a & b; }
    template <typename T>
    T default_or(T a, T b) { return a | b; }
}

#line 4 "library/KowerKoint/matrix.hpp"

template <
    typename T,
    T (*add)(const T, const T)=internal_operator::default_add,
    T (*zero)()=internal_operator::zero,
    T (*mult)(const T, const T)=internal_operator::default_mult,
    T (*one)()=internal_operator::one,
    T (*sub)(const T, const T)=internal_operator::default_sub,
    T (*div)(const T, const T)=internal_operator::default_div
>
struct Matrix {
    int n, m;
    vector<vector<T>> A;

    Matrix() : n(0), m(0), A(vector<vector<T>>(0)) {}
    Matrix(size_t _n, size_t _m) : n(_n), m(_m), A(_n, vector<T>(_m, zero())) {}
    Matrix(vector<vector<T>> _A) : n(_A.size()), m(_A[0].size()), A(_A) {}

    vector<T> &operator[](int i) { return A.at(i); }
    const vector<T> &operator[](int i) const { return A.at(i); }

    static Matrix I(size_t n) {
        Matrix ret(n, n);
        REP(i, n) ret[i][i] = one();
        return ret;
    }

    Matrix &operator+=(const Matrix &B) {
        assert(n == B.n && m == B.m);
        REP(i, n) REP(j, m) A[i][j] = add(A[i][j], B[i][j]);
        return *this;
    }
    Matrix operator+(const Matrix &B) const {
        return (Matrix(*this) += B);
    }

    Matrix &operator-=(const Matrix &B) {
        assert(n == B.n && m == B.m);
        REP(i, n) REP(j, m) A[i][j] = sub(A[i][j], B[i][j]);
        return *this;
    }
    Matrix operator-(const Matrix &B) const {
        return (Matrix(*this) -= B);
    }

    Matrix &operator*=(const Matrix &B) {
        assert(m == B.n);
        vector<vector<T>> res(n, vector<T>(B.m, zero()));
        REP(i, n) REP(j, m) REP(k, B.m) res[i][k] = add(res[i][k], mult(A[i][j], B[j][k]));
        A.swap(res);
        m = B.m;
        return (*this);
    }
    Matrix operator*(const Matrix &B) const {
        return (Matrix(*this) *= B);
    }

    Matrix &operator|=(const Matrix &B) {
        assert(B.n == n);
        REP(i, n) {
            A[i].resize(m+B.m);
            REP(j, B.m) A[i][m+j] = B[i][j];
        }
        m += B.m;
        return (*this);
    }
    Matrix operator|(const Matrix &B) const {
        return (Matrix(*this) |= B);
    }

    Matrix &operator|=(const vector<T> &B) {
        assert(B.size() == n);
        REP(i, n) {
            A[i].push_back(B[i]);
        }
        m++;
        return (*this);
    }
    Matrix operator|(const vector<T> &B) const {
        return (Matrix(*this) |= B);
    }

    Matrix &operator&=(const Matrix &B) {
        assert(B.m == m);
        A.resize(n+B.n);
        REP(i, B.n) {
            A[n+i] = B[i];
        }
        n += B.n;
        return (*this);
    }
    Matrix operator&(const Matrix &B) const {
        return (Matrix(*this) &= B);
    }

    Matrix &operator&=(const vector<T> &B) {
        assert(B.size() == m);
        A.push_back(B);
        n++;
        return (*this);
    }
    Matrix operator&(const vector<T> &B) const {
        return (Matrix(*this) &= B);
    }

    friend istream &operator>>(istream &is, Matrix &mat) {
        REP(i, mat.n) REP(j, mat.m) is >> mat[i][j];
        return is;
    }

    friend ostream &operator<<(ostream &os, const Matrix &mat) {
        REP(i, mat.n) {
            REP(j, mat.m) os << mat[i][j] << (j==mat.m-1? '\n' : ' ');
        }
        return os;
    }

    pair<Matrix, T> gaussian_elimination() const {
        Matrix mat(*this);
        T det = one();
        VI columns;
        int i = 0;
        int j = 0;
        while(i < n && j < m) {
            int idx = -1;
            FOR(k, i, n) if(mat[k][j] != zero()) idx = k;
            if(idx == -1) {
                det = zero();
                j++;
                continue;
            }
            if(i != idx) {
                det *= sub(zero(), one());
                swap(mat[i], mat[idx]);
            }
            det *= mat[i][j];
            T scale = mat[i][j];
            REP(l, m) mat[i][l] = div(mat[i][l], scale);
            FOR(k, i+1, n) {
                T scale = mat[k][j];
                REP(l, m) mat[k][l] = sub(mat[k][l], mult(mat[i][l], scale));
            }
            columns.push_back(j);
            i++;
            j++;
        }
        REP(i, columns.size()) {
            int j = columns[i];
            REP(k, i) {
                T scale = mat[k][j];
                FOR(l, j, m) {
                    mat[k][l] = sub(mat[k][l], mult(mat[i][l], scale));
                }
            }
        }
        return make_pair(mat, det);
    }

    void make_basis() {
        *this = gaussian_elimination().first;
        while(n && get_bra(n-1) == vector<T>(m, zero())) pop_bra();
    }

    Matrix inv() const {
        Matrix and_i = A | I(n);
        auto [i_and, det] = and_i.gaussian_elimination();
        assert(det != zero());
        Matrix res(n, n);
        REP(i, n) REP(j, n) res[i][j] = i_and[i][n+i];
        return res;
    }

    vector<T> get_bra(int i) const {
        assert(0 <= i && i < n);
        return A[i];
    }

    vector<T> get_ket(int i) const {
        assert(0 <= i && i < m);
        vector<T> res(n);
        REP(i, n) res[i] = A[i][i];
        return res;
    }

    void pop_bra() {
        assert(n > 0);
        A.pop_back();
        n--;
    }

    void pop_ket() {
        assert(m > 0);
        REP(i, n) A[i].pop_back();
        m--;
    }

    Matrix transpose() const {
        Matrix res(m, n);
        REP(i, n) REP(j, m) res[j][i] = A[i][j];
        return res;
    }

    Matrix operator^=(ll n) {
        if(n < 0) {
            *this = this->inv();
            n = -n;
        }
        Matrix res = Matrix::I(n);
        while(n) {
            if(n & 1) res *= *this;
            *this *= *this;
            n >>= 1LL;
        }
        A.swap(res.A);
        return (*this);
    }
    Matrix operator^(const ll n) const {
        return (Matrix(*this) ^= n);
    }
};

using XorMatrix = Matrix<
    int,
    internal_operator::default_xor<int>,
    internal_operator::zero<int>,
    internal_operator::default_and<int>,
    internal_operator::one<int>,
    internal_operator::default_xor<int>,
    internal_operator::default_and<int>
>;
#line 2 "library/KowerKoint/test/yukicoder-184/main.cpp"

int main(void) {
    int n; cin >> n;
    VL a(n); cin >> a;
    XorMatrix mat(61, n);
    REP(i, 61) REP(j, n) mat[i][j] = a[j] >> i & 1LL;
    auto basis = mat.gaussian_elimination().first;
    ll ans = 1;
    REP(i, 61) {
        REP(j, n) if(basis[i][j]) {
            ans <<= 1LL;
            break;
        }
    }
    print(ans);
}
0