結果
| 問題 |
No.1240 Or Sum of Xor Pair
|
| ユーザー |
|
| 提出日時 | 2020-09-26 00:20:14 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 463 ms / 2,000 ms |
| コード長 | 1,562 bytes |
| コンパイル時間 | 1,859 ms |
| コンパイル使用メモリ | 197,592 KB |
| 最終ジャッジ日時 | 2025-01-14 21:47:35 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:56:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
56 | scanf("%d", &A[i]);
| ~~~~~^~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct BinaryTrie{
int nth_bit(int x, int n){ return (x>>n) & 1; }
struct Node{
Node* ch[2];
int num[18][2];
Node(){
ch[0] = ch[1] = nullptr;
memset(num, 0, sizeof(num));
}
};
int B;
Node* root;
BinaryTrie(int B=0){ if(B) initialize(B); }
void initialize(int inB){
B = inB;
root = new Node();
}
void insert(Node* node, int x, int depth){
for(int k=0; k<18; k++) node->num[k][nth_bit(x, k)]++;
if(depth < B){
int nextdepth = depth + 1;
int nextbit = nth_bit(x, B - nextdepth);
if(!node->ch[nextbit]) node->ch[nextbit] = new Node();
insert(node->ch[nextbit], x, nextdepth);
}
}
void insert(int x){ insert(root, x, 0); }
void calc(Node* node, int x, int now, int K, int X, int64_t& ans){
if(!node) return;
int mn = now, mx = now + (1<<K) - 1;
if(mn >= X) return;
if(mx < X){
for(int k=0; k<18; k++) for(int t=0; t<2; t++) ans += node->num[k][t] * (1LL<<k) * (nth_bit(x, k) | t);
return;
}
if(K > 0) for(int t=0; t<2; t++) calc(node->ch[t], x, now + (1<<(K-1)) * (nth_bit(x, K-1) ^ t), K-1, X, ans);
}
void calc(int x, int X, int64_t& ans){ return calc(root, x, 0, B, X, ans); }
};
int main(){
int N, X;
cin >> N >> X;
BinaryTrie bt(18);
vector<int> A(N);
for(int i=0; i<N; i++){
scanf("%d", &A[i]);
bt.insert(A[i]);
}
int64_t ans = 0;
for(int a : A){
ans -= a;
bt.calc(a, X, ans);
}
ans /= 2;
cout << ans << endl;
}