結果

問題 No.184 たのしい排他的論理和(HARD)
ユーザー 👑 emthrmemthrm
提出日時 2020-01-04 12:54:26
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 60 ms / 5,000 ms
コード長 3,258 bytes
コンパイル時間 3,037 ms
コンパイル使用メモリ 195,812 KB
最終ジャッジ日時 2025-01-08 16:19:47
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
template <typename T> using posteriority_queue = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
int popcount(int val) { return __builtin_popcount(val); }
int popcountll(ll val) { return __builtin_popcountll(val); }
template <typename T> void unique(vector<T> &a) { a.erase(unique(ALL(a)), a.end()); }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

const int COL = 61;
struct BinaryMatrix {
  int m, n;

  BinaryMatrix(int m, int n = COL, bool def = false) : m(m), n(n), dat(m, bitset<COL>(0)) {
    if (def) {
      REP(i, m) REP(j, n) dat[i][j] = 1;
    }
  }

  BinaryMatrix pow(ll exponent) {
    BinaryMatrix tmp = *this, res(n, n);
    REP(i, n) res[i][i] = 1;
    while (exponent > 0) {
      if (exponent & 1) res *= tmp;
      tmp *= tmp;
      exponent >>= 1;
    }
    return res;
  }

  inline const bitset<COL> &operator[](const int idx) const { return dat[idx]; }
  inline bitset<COL> &operator[](const int idx) { return dat[idx]; }

  BinaryMatrix &operator=(const BinaryMatrix &rhs) {
    m = rhs.m;
    n = rhs.n;
    dat.resize(m);
    REP(i, m) dat[i] = rhs[i];
    return *this;
  }

  BinaryMatrix &operator+=(const BinaryMatrix &rhs) {
    REP(i, m) dat[i] ^= rhs[i];
    return *this;
  }

  BinaryMatrix &operator*=(const BinaryMatrix &rhs) {
    int height = m, width = rhs.n;
    BinaryMatrix t_rhs(rhs.n, rhs.m), res(height, width);
    REP(i, rhs.n) REP(j, rhs.m) t_rhs[i][j] = rhs[j][i];
    REP(i, height) REP(j, width) res[i][j] = ((dat[i] & t_rhs[j]).count() & 1);
    *this = res;
    return *this;
  }

  BinaryMatrix operator+(const BinaryMatrix &rhs) const { return BinaryMatrix(*this) += rhs; }

  BinaryMatrix operator*(const BinaryMatrix &rhs) const { return BinaryMatrix(*this) *= rhs; }

private:
  vector<bitset<COL> > dat;
};

int gauss_jordan(BinaryMatrix &mat, bool is_extended = false) {
  int rank = 0;
  REP(col, mat.n) {
    if (is_extended && col == mat.n - 1) break;
    int pivot = -1;
    FOR(row, rank, mat.m) {
      if (mat[row][col]) {
        pivot = row;
        break;
      }
    }
    if (pivot == -1) continue;
    swap(mat[rank], mat[pivot]);
    REP(row, mat.m) {
      if (row != rank && mat[row][col]) mat[row] ^= mat[rank];
    }
    ++rank;
  }
  return rank;
}

int main() {
  int n; cin >> n;
  BinaryMatrix mat(n);
  REP(i, n) {
    ll a; cin >> a;
    mat[i] = bitset<COL>(a);
  }
  int rank = gauss_jordan(mat);
  ll ans = 1;
  REP(_, rank) ans <<= 1;
  cout << ans << '\n';
  return 0;
}
0