結果
| 問題 |
No.1240 Or Sum of Xor Pair
|
| コンテスト | |
| ユーザー |
aajisaka
|
| 提出日時 | 2020-09-26 02:10:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,124 bytes |
| コンパイル時間 | 4,579 ms |
| コンパイル使用メモリ | 197,372 KB |
| 最終ジャッジ日時 | 2025-01-14 21:52:26 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 3 WA * 27 |
ソースコード
/**
* 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<ll> a(1<<18);
rep(i, n) {
int ai; cin >> ai;
a[ai]++;
}
ll ret = 0;
vector<ll> A(1<<18);
auto B = A;
ll cnt = 0;
A = a;
fwt_xor(A);
rep(i, 1<<18) {
B[i] = A[i]*A[i];
}
ifwt_xor(B);
for(int i=1; i<x; i++) {
cnt += B[i]/2;
}
debug(cnt);
for(int k=0; k<18; k++) {
rep(i, 1LL<<18) {
if ((i & (1LL << k)) == 0) {
A[i] = a[i];
} else {
A[i] = 0;
}
}
fwt_xor(A);
rep(i, 1<<18) B[i] = A[i]*A[i];
ifwt_xor(B);
ll cnt2 = 0;
for(int i=1; i<x; i++) {
cnt2 += B[i]/2;
//debug(k, i, B[i]/2);
}
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;
}
aajisaka