結果

問題 No.184 たのしい排他的論理和(HARD)
ユーザー not_522not_522
提出日時 2015-08-20 00:14:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 338 ms / 5,000 ms
コード長 6,408 bytes
コンパイル時間 1,738 ms
コンパイル使用メモリ 167,324 KB
実行使用メモリ 15,744 KB
最終ジャッジ日時 2024-07-04 12:02:25
合計ジャッジ時間 8,639 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 237 ms
12,032 KB
testcase_09 AC 51 ms
5,376 KB
testcase_10 AC 181 ms
9,856 KB
testcase_11 AC 130 ms
7,936 KB
testcase_12 AC 275 ms
13,568 KB
testcase_13 AC 297 ms
14,464 KB
testcase_14 AC 172 ms
9,472 KB
testcase_15 AC 326 ms
15,360 KB
testcase_16 AC 269 ms
13,184 KB
testcase_17 AC 293 ms
14,080 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 85 ms
14,080 KB
testcase_21 AC 336 ms
15,744 KB
testcase_22 AC 338 ms
15,616 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 200 ms
10,624 KB
testcase_29 AC 282 ms
13,696 KB
testcase_30 AC 247 ms
12,672 KB
testcase_31 AC 212 ms
11,520 KB
testcase_32 AC 267 ms
13,312 KB
testcase_33 AC 329 ms
15,360 KB
testcase_34 AC 320 ms
15,360 KB
testcase_35 AC 334 ms
15,616 KB
testcase_36 AC 331 ms
15,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

namespace arithmetic {
  template<typename T> class Addition {
  public:
    template<typename V> T operator+(const V& v) const {
      return T(static_cast<const T&>(*this)) += v;
    }
  };

  template<typename T> class Subtraction {
  public:
    template<typename V> T operator-(const V& v) const {
      return T(static_cast<const T&>(*this)) -= v;
    }
  };

  template<typename T> class Multiplication {
  public:
    template<typename V> T operator*(const V& v) const {
      return T(static_cast<const T&>(*this)) *= v;
    }
  };

  template<typename T> class Division {
  public:
    template<typename V> T operator/(const V& v) const {
      return T(static_cast<const T&>(*this)) /= v;
    }
  };

  template<typename T> class Modulus {
  public:
    template<typename V> T operator%(const V& v) const {
      return T(static_cast<const T&>(*this)) %= v;
    }
  };
}

template<typename T> class IndivisibleArithmetic : public arithmetic::Addition<T>, public arithmetic::Subtraction<T>, public arithmetic::Multiplication<T> {};

template<typename T> class Arithmetic : public IndivisibleArithmetic<T>, public arithmetic::Division<T> {};

template<typename T> class Bitwise {
public:
  T operator&(const T& v) const {
    T res(static_cast<const T&>(*this));
    return res &= v;
  }

  T operator|(const T& v) const {
    T res(static_cast<const T&>(*this));
    return res |= v;
  }

  T operator^(const T& v) const {
    T res(static_cast<const T&>(*this));
    return res ^= v;
  }
};

template<typename T> int least_bit(T n) {
  static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size");
  if (sizeof(T) == 4) return __builtin_ffs(n) - 1;
  if (sizeof(T) == 8) return __builtin_ffsll(n) - 1;
}

template<typename T> int most_bit(T n) {
  static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size");
  if (sizeof(T) == 4) return n ? 31 - __builtin_clz(n) : -1;
  if (sizeof(T) == 8) return n ? 63 - __builtin_clzll(n) : -1;
}

template<typename T> int count_bit(T n) {
  static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size");
  if (sizeof(T) == 4) return __builtin_popcount(n);
  if (sizeof(T) == 8) return __builtin_popcountll(n);
}

class Bitset : public Bitwise<Bitset> {
private:
  int arraySize(int n) const {
    return (n + 63) / 64;
  }

  void resize(int n) {
    val.resize(arraySize(n) + 1);
  }

public:
  vector<unsigned long long> val;

  class reference {
  private:
    friend class Bitset;
    vector<unsigned long long>::iterator val;
    int pos;
    reference(vector<unsigned long long>::iterator val, int pos) : val(val), pos(pos) {}

  public:
    reference operator=(const reference& r) {
      if ((bool)r) *val |= 1ull << pos;
      else *val &= ~(1ull << pos);
      return *this;
    }

    reference operator=(const bool b) {
      if (b) *val |= 1ull << pos;
      else *val &= ~(1ull << pos);
      return *this;
    }

    operator bool() const {
      return *val >> pos & 1;
    }
  };

  Bitset() {}

  Bitset(int n) : val(arraySize(n), 0) {}

  reference operator[](int n) {
    if (arraySize(n) >= (int)val.size()) resize(n);
    return reference(val.begin() + n / 64, n % 64);
  }

  bool operator[](int n) const {
    if (arraySize(n) >= (int)val.size()) return false;
    return val[n / 64] >> n % 64 & 1;
  }

  Bitset operator&=(const Bitset& b) {
    if (val.size() < b.val.size()) val.resize(b.val.size());
    for (size_t i = 0; i < val.size(); ++i) {
      if (i < b.val.size()) val[i] &= b.val[i];
    }
    return *this;
  }

  Bitset operator|=(const Bitset& b) {
    if (val.size() < b.val.size()) val.resize(b.val.size());
    for (size_t i = 0; i < val.size(); ++i) {
      if (i < b.val.size()) val[i] |= b.val[i];
    }
    return *this;
  }

  Bitset operator^=(const Bitset& b) {
    if (val.size() < b.val.size()) val.resize(b.val.size());
    for (size_t i = 0; i < val.size(); ++i) {
      if (i < b.val.size()) val[i] ^= b.val[i];
    }
    return *this;
  }

  int count() {
    return accumulate(val.begin(), val.end(), 0, [](int a, unsigned long long b){return a + count_bit(b);});
  }

  bool parity() {
    return accumulate(val.begin(), val.end(), 0, [](int a, unsigned long long b){return a ^ __builtin_parityll(b);});
  }

  int size() const {
    return val.size() * 64;
  }
};

class BitsetMatrix : public Arithmetic<BitsetMatrix> {
private:
  vector<Bitset> val;

public:
  BitsetMatrix(int n) : val(n) {}

  BitsetMatrix(int n, int m) : val(n, Bitset(m)) {}

  Bitset& operator[](int n) {
    return val[n];
  }

  const Bitset& operator[](int n) const {
    return val[n];
  }

	BitsetMatrix operator+=(const BitsetMatrix& m) {
    for (int i = 0; i < size(); ++i) val[i] ^= m[i];
		return *this;
	}

	BitsetMatrix operator-=(const BitsetMatrix& m) {
    for (int i = 0; i < size(); ++i) val[i] ^= m[i];
		return *this;
	}

  BitsetMatrix operator*=(const BitsetMatrix& _m) {
    BitsetMatrix m = _m.transpose(), res(size());
    for (int i = 0; i < size(); ++i) res[i] = *this * m[i];
    return *this = res.transpose();
  }

  BitsetMatrix operator*(const BitsetMatrix& m) const {
    auto res(static_cast<const BitsetMatrix&>(*this));
    return res *= m;
  }

  Bitset operator*(const Bitset& v) const {
    Bitset res(size());
    for (int i = 0; i < size(); ++i) res[i] = (val[i] & v).parity();
    return res;
  }

  BitsetMatrix transpose() const {
    BitsetMatrix res(size());
    for (int i = 0; i < size(); ++i) {
      for (int j = 0; j < size(); ++j) {
        res[i][j] = (*this)[j][i];
      }
    }
    return res;
  }

  int size() const {
    return val.size();
  }

  int rank() const {
    int n = 0, r = 0;
    BitsetMatrix mat = *this;
    for (int i = 0; i < size(); ++i) n = max(n, mat[i].size());
    for (int i = 0; i < n; ++i) {
      int p = r;
      for (int j = r + 1; j < size(); ++j) {
        if (abs(mat[j][i]) > abs(mat[p][i])) p = j;
      }
      if (mat[p][i] == 0) continue;
      swap(mat[r], mat[p]);
      for (int j = r + 1; j < size(); ++j) {
        if (!mat[j][i]) continue;
        mat[j] ^= mat[r];
      }
      ++r;
      if (r == size()) break;
    }
    return r;
  }
};

int main() {
  int n;
  cin >> n;
  BitsetMatrix m(n);
  for (int i = 0; i < n; ++i) {
    long long a;
    cin >> a;
    for (int j = 0; a; ++j, a /= 2) m[i][j] = a % 2;
  }
  cout << (1ll << m.rank()) << endl;
}
0