結果
| 問題 |
No.1240 Or Sum of Xor Pair
|
| コンテスト | |
| ユーザー |
carrot46
|
| 提出日時 | 2020-09-26 00:32:18 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,300 bytes |
| コンパイル時間 | 1,838 ms |
| コンパイル使用メモリ | 174,392 KB |
| 実行使用メモリ | 15,100 KB |
| 最終ジャッジ日時 | 2024-06-28 08:29:56 |
| 合計ジャッジ時間 | 5,265 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 16 WA * 14 |
ソースコード
#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<58);
ll Res(0);
ll calc(bool one, vec &a, vec &b){
ll res;
if(one){
res = accumulate(ALL(a), 0LL) * (a.size() - 1);
rep(i, 18) {
int num(0);
for(ll u : a) if((u>>i)&1) ++num;
res -= (1LL<<i) * (num * (num - 1) / 2);
}
return res;
}else{
res = accumulate(ALL(a), 0LL) * b.size() + accumulate(ALL(b), 0LL) * a.size();
rep(i, 18) {
int num(0);
for(ll u : a) if((u>>i)&1) ++num;
for(ll u : b) if((u>>i)&1) res -= (1LL<<i) * num;
}
return res;
}
}
void dfs(bool larger_than_X, int bit, vec &a, vec &b){
if(bit == -1 || a.empty() || (!larger_than_X && b.empty())) return;
vec a_zero(0), a_one(0), b_zero(0), b_one(0), blank(0);
rep(_, 2) {
for (ll u : a) {
if ((u >> bit) & 1) a_one.push_back(u);
else a_zero.push_back(u);
}
swap(a, b); swap(a_one, b_one); swap(a_zero, b_zero);
}
if(larger_than_X){
if((K>>bit)&1){
Res += calc(true, a_zero, blank) + calc(true, a_one, blank);
dfs(false, bit - 1, a_zero, a_one);
}else{
dfs(true, bit - 1, a_zero, blank);
dfs(true, bit - 1, a_one, blank);
}
}else{
if((K>>bit)&1){
Res += calc(false, a_zero, b_zero) + calc(false, a_one, b_one);
dfs(false, bit - 1, a_zero, b_one);
dfs(false, bit - 1, a_one, b_zero);
}else{
dfs(false, bit - 1, a_zero, b_zero);
dfs(false, bit - 1, a_one, b_one);
}
}
}
int main() {
cin>>N>>K;
vec a(N), b(0);
rep(i, N) cin>>a[i];
if(K == (1LL<<18)){
cout<<calc(true, a, b)<<endl;
}else{
dfs(true, 17, a, b);
cout<<Res<<endl;
}
}
carrot46