結果

問題 No.184 たのしい排他的論理和(HARD)
ユーザー hashiryohashiryo
提出日時 2020-04-22 23:18:45
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 159 ms / 5,000 ms
コード長 6,271 bytes
コンパイル時間 1,954 ms
コンパイル使用メモリ 187,580 KB
実行使用メモリ 60,056 KB
最終ジャッジ日時 2023-09-17 18:13:35
合計ジャッジ時間 6,240 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 112 ms
43,592 KB
testcase_09 AC 25 ms
11,548 KB
testcase_10 AC 86 ms
33,876 KB
testcase_11 AC 62 ms
25,272 KB
testcase_12 AC 131 ms
49,972 KB
testcase_13 AC 142 ms
53,944 KB
testcase_14 AC 81 ms
32,136 KB
testcase_15 AC 152 ms
58,212 KB
testcase_16 AC 127 ms
49,032 KB
testcase_17 AC 138 ms
52,608 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 89 ms
59,988 KB
testcase_21 AC 158 ms
60,056 KB
testcase_22 AC 156 ms
59,992 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 95 ms
37,308 KB
testcase_29 AC 135 ms
51,676 KB
testcase_30 AC 119 ms
46,128 KB
testcase_31 AC 102 ms
40,208 KB
testcase_32 AC 128 ms
49,548 KB
testcase_33 AC 154 ms
58,476 KB
testcase_34 AC 154 ms
57,932 KB
testcase_35 AC 159 ms
59,528 KB
testcase_36 AC 157 ms
59,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <int mod>
struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod)) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) { return *this *= p.inverse(); }
  ModInt operator-() const { return ModInt() - *this; }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b) t = a / b, swap(a -= t * b, b), swap(u -= t * v, v);
    return ModInt(u);
  }
  ModInt pow(int64_t e) const {
    ModInt ret(1);
    for (ModInt b = *this; e; e >>= 1, b *= b)
      if (e & 1) ret *= b;
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int modulo() { return mod; }
};

struct BitMatrix {
 private:
  vector<vector<short>> a;

 public:
  BitMatrix() {}
  BitMatrix(size_t n, size_t m) : a(n, vector<short>(m, 0)) {}
  BitMatrix(size_t n) : BitMatrix(n, n) {}
  inline const vector<short> &operator[](size_t k) const { return a[k]; }
  inline vector<short> &operator[](size_t k) { return a[k]; }
  size_t height() const { return a.size(); }
  size_t width() const { return a[0].size(); }

  static BitMatrix I(size_t n) {
    BitMatrix mat(n);
    for (int i = 0; i < n; i++) mat[i][i] = 1;
    return mat;
  }
  BitMatrix operator+(const BitMatrix &b) const {
    size_t n = height(), m = width();
    BitMatrix c(n, m);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) c[i][j] = (*this)[i][j] ^ b[i][j];
    return c;
  }
  BitMatrix operator*(const BitMatrix &b) const {
    if (width() <= 64) return mul<64>(b);
    if (width() <= 2600) return mul<2600>(b);
    return mul<100010>(b);
  }
  BitMatrix &operator+=(const BitMatrix &b) { return *this = (*this) + b; }
  BitMatrix &operator*=(const BitMatrix &b) { return *this = (*this) * b; }
  bool operator==(const BitMatrix &b) const { return a == b.a; }
  BitMatrix pow(uint64_t e) const {
    BitMatrix ret = I(height());
    for (BitMatrix base = *this; e; e >>= 1, base *= base)
      if (e & 1) ret *= base;
    return ret;
  }

  static pair<BitMatrix, BitMatrix> Gauss_Jordan(const BitMatrix &a,
                                                 const BitMatrix &b) {
    if (a.width() + b.width() <= 64) return gauss_jordan_content<64>(a, b);
    if (a.width() + b.width() <= 2600) return gauss_jordan_content<2600>(a, b);
    return gauss_jordan_content<100010>(a, b);
  }
  static pair<vector<int>, vector<vector<int>>> linear_equations(
      const BitMatrix &a, const vector<int> &b) {
    int n = a.height(), m = a.width();
    BitMatrix B(n, 1);
    for (int i = 0; i < n; i++) B[i][0] = b[i];
    auto p = Gauss_Jordan(a, B);
    vector<int> jdx(n, -1), idx(m, -1);
    for (int i = 0, j; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (p.first[i][j]) {
          jdx[i] = j, idx[j] = i;
          break;
        }
      }
      if (j == m && p.second[i][0])
        return make_pair(vector<int>(), vector<vector<int>>());  // no solutions
    }
    vector<int> c(m);
    vector<vector<int>> d;
    for (int j = 0; j < m; j++) {
      if (idx[j] != -1)
        c[j] = p.second[idx[j]][0];
      else {
        vector<int> v(m);
        v[j] = 1;
        for (int i = 0; i < n; i++)
          if (jdx[i] != -1) v[jdx[i]] = p.first[i][j];
        d.push_back(v);
      }
    }
    return make_pair(c, d);
  }
  int rank() const {
    int n = height(), m = width();
    BitMatrix b(n, 0);
    BitMatrix p = Gauss_Jordan(*this, b).first;
    for (int i = 0, j; i < n; i++) {
      for (j = 0; j < m; j++)
        if (p[i][j] != 0) break;
      if (j == m) return i;
    }
    return n;
  }

 private:
  template <size_t SIZE>
  BitMatrix mul(const BitMatrix &b) const {
    size_t n = height(), m = width(), l = b.width();
    assert(m == b.height());
    vector<bitset<SIZE>> tb(l);
    for (int i = 0; i < l; ++i)
      for (int j = 0; j < m; ++j) tb[i][j] = b[j][i];
    BitMatrix c(n, l);
    for (int i = 0; i < n; i++) {
      bitset<SIZE> abit;
      for (int k = 0; k < m; k++) abit[k] = (*this)[i][k];
      for (int j = 0; j < l; j++) c[i][j] = ((abit & tb[j]).count() & 1);
    }
    return c;
  }

  template <size_t SIZE>
  static pair<BitMatrix, BitMatrix> gauss_jordan_content(const BitMatrix &a,
                                                         const BitMatrix &b) {
    size_t n = a.height(), m = a.width(), l = b.width();
    vector<bitset<SIZE>> c(n);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) c[i][j] = a[i][j];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < l; j++) c[i][j + m] = b[i][j];
    int d = 0;
    for (int j = 0; j < m; j++) {
      int p = d;
      for (int i = d + 1; i < n; i++)
        if (c[i][j]) p = i;
      if (!c[p][j]) continue;
      swap(c[p], c[d]);
      for (int i = 0; i < n; i++)
        if (i != d && c[i][j]) c[i] ^= c[d];
      d++;
    }
    BitMatrix reta(n, m), retb(n, l);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) reta[i][j] = c[i][j];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < l; j++) retb[i][j] = c[i][j + m];
    return make_pair(reta, retb);
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N;
  cin >> N;
  BitMatrix A(N, 61);
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    for (int j = 0; j <= 60; j++) A[i][j] = (a >> j) & 1;
  }
  cout << (1ll << A.rank()) << endl;
  return 0;
}
0