結果
問題 | No.1240 Or Sum of Xor Pair |
ユーザー |
![]() |
提出日時 | 2020-09-26 02:32:15 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 396 ms / 2,000 ms |
コード長 | 4,171 bytes |
コンパイル時間 | 2,995 ms |
コンパイル使用メモリ | 196,904 KB |
最終ジャッジ日時 | 2025-01-14 21:53:06 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
/** * 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; }