結果

問題 No.1240 Or Sum of Xor Pair
ユーザー aajisakaaajisaka
提出日時 2020-09-26 02:32:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 319 ms / 2,000 ms
コード長 4,171 bytes
コンパイル時間 2,305 ms
コンパイル使用メモリ 202,760 KB
実行使用メモリ 6,236 KB
最終ジャッジ日時 2023-09-10 19:54:05
合計ジャッジ時間 13,705 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 273 ms
5,192 KB
testcase_01 AC 275 ms
5,204 KB
testcase_02 AC 276 ms
5,192 KB
testcase_03 AC 280 ms
5,160 KB
testcase_04 AC 280 ms
5,160 KB
testcase_05 AC 279 ms
5,400 KB
testcase_06 AC 279 ms
5,160 KB
testcase_07 AC 279 ms
5,192 KB
testcase_08 AC 280 ms
5,132 KB
testcase_09 AC 279 ms
5,076 KB
testcase_10 AC 282 ms
5,068 KB
testcase_11 AC 285 ms
5,108 KB
testcase_12 AC 285 ms
5,080 KB
testcase_13 AC 286 ms
5,264 KB
testcase_14 AC 288 ms
5,064 KB
testcase_15 AC 318 ms
5,980 KB
testcase_16 AC 319 ms
5,860 KB
testcase_17 AC 317 ms
6,060 KB
testcase_18 AC 316 ms
5,956 KB
testcase_19 AC 317 ms
5,980 KB
testcase_20 AC 317 ms
5,984 KB
testcase_21 AC 318 ms
6,236 KB
testcase_22 AC 317 ms
5,856 KB
testcase_23 AC 319 ms
6,236 KB
testcase_24 AC 319 ms
5,988 KB
testcase_25 AC 319 ms
5,952 KB
testcase_26 AC 317 ms
5,984 KB
testcase_27 AC 276 ms
5,220 KB
testcase_28 AC 275 ms
5,160 KB
testcase_29 AC 303 ms
5,920 KB
testcase_30 AC 290 ms
5,532 KB
testcase_31 AC 289 ms
5,448 KB
testcase_32 AC 297 ms
5,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */


#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// Fast Walsh-Hadamard transform
// Original: https://kazuma8128.hatenablog.com/entry/2018/05/31/144519

// Get C[k] = Sum(A[i]*B[j]) (i^j=k) by O(nlogn)
// n must be 2^p (p: non-negative integer)
// fwt_xor(A); fwt_xor(B);
// rep(i, n) C[i] = A[i]*B[i];
// ifwt_xor(C);
template <typename T>
void fwt_xor(vector<T>& f) {
  int n = f.size();
  for (int i = 1; i < n; i <<= 1) {
    for (int j = 0; j < n; j++) {
      if ((j & i) == 0) {
        T x = f[j], y = f[j | i];
        f[j] = x + y, f[j | i] = x - y;
      }
    }
  }
}
template <typename T>
void ifwt_xor(vector<T>& f) {
  int n = f.size();
  for (int i = 1; i < n; i <<= 1) {
    for (int j = 0; j < n; j++) {
      if ((j & i) == 0) {
        T x = f[j], y = f[j | i];
        f[j] = (x + y) / 2, f[j | i] = (x - y) / 2;
      }
    }
  }
}

// Get C[k] = Sum(A[i]*B[j]) (i|j=k) by O(nlogn)
// n must be 2^p (p: non-negative integer)
// fwt_or(A); fwt_or(B);
// rep(i, n) C[i] = A[i]*B[i];
// ifwt_or(C);
template <typename T>
void fwt_or(vector<T>& f) {
  int n = f.size();
  for (int i = 1; i < n; i <<= 1) {
    for (int j = 0; j < n; j++) {
      if ((j & i) == 0) {
        f[j | i] += f[j];
      }
    }
  }
}
template <typename T>
void ifwt_or(vector<T>& f) {
  int n = f.size();
  for (int i = 1; i < n; i <<= 1) {
    for (int j = 0; j < n; j++) {
      if ((j & i) == 0) {
        f[j | i] -= f[j];
      }
    }
  }
}

// Get C[k] = Sum(A[i]*B[j]) (i&j=k) by O(nlogn)
// n must be 2^p (p: non-negative integer)
// fwt_and(A); fwt_and(B);
// rep(i, n) C[i] = A[i]*B[i];
// ifwt_and(C);
template <typename T>
void fwt_and(vector<T>& f) {
  int n = f.size();
  for (int i = 1; i < n; i <<= 1) {
    for (int j = 0; j < n; j++) {
      if ((j & i) == 0) {
        f[j] += f[j | i];
      }
    }
  }
}
template <typename T>
void ifwt_and(vector<T>& f) {
  int n = f.size();
  for (int i = 1; i < n; i <<= 1) {
    for (int j = 0; j < n; j++) {
      if ((j & i) == 0) {
        f[j] -= f[j | i];
      }
    }
  }
}


class No1240OrSumOfXorPair {
public:
    void solve(istream& cin, ostream& cout) {
      SPEED;
      int n, x; cin >> n >> x;
      vector<int> a(n);
      rep(i, n) {
        cin >> a[i];
      }
      ll ret = 0;

      vector<ll> A(1<<18);
      rep(i, n) {
        A[a[i]]++;
      }

      ll cnt = 0;
      fwt_xor(A);
      rep(i, 1<<18) {
        A[i] *= A[i];
      }
      ifwt_xor(A);
      A[0] -= n; // i == j
      for(int i=0; i<x; i++) {
        cnt += A[i]/2;
      }
      debug(cnt);


      for(int k=0; k<18; k++) {
        int used = 0;
        rep(i, 1<<18) A[i] = 0;
        rep(i, n) {
          if ((a[i]&(1<<k)) == 0) {
            A[a[i]]++;
            used++;
          }
        }
        fwt_xor(A);
        rep(i, 1<<18) A[i] *= A[i];
        ifwt_xor(A);
        ll cnt2 = 0;
        A[0] -= used;
        for(int i=0; i<x; i++) {
          cnt2 += A[i]/2;
          debug(k, i, A[i]);
        }
        ret += (cnt-cnt2)*(1LL<<k);
      }

      cout << ret << endl;
    }
};

signed main() {
  No1240OrSumOfXorPair solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
0