結果

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

ソースコード

diff #

#include <bitset>
#include <iostream>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
/*-------------------------------------------------*/
const int MAX = 61;
struct BinaryMatrix {
  int m, n;

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

  BinaryMatrix pow(long long 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<MAX> &operator[](const int idx) const { return dat[idx]; }
  inline bitset<MAX> &operator[](const int idx) { return dat[idx]; }

  BinaryMatrix &operator=(const BinaryMatrix &rhs) {
    m = rhs.m;
    n = rhs.n;
    dat.clear();
    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<MAX> > 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() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n; cin >> n;
  BinaryMatrix mat(n);
  REP(i, n) {
    long long a; cin >> a;
    mat[i] = bitset<MAX>(a);
  }
  int rank = gauss_jordan(mat);
  long long ans = 1;
  REP(i, rank) ans *= 2;
  cout << ans << '\n';
  return 0;
}
0