結果

問題 No.1240 Or Sum of Xor Pair
ユーザー carrot46carrot46
提出日時 2020-09-26 00:32:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,300 bytes
コンパイル時間 2,088 ms
コンパイル使用メモリ 170,184 KB
実行使用メモリ 14,772 KB
最終ジャッジ日時 2023-09-10 17:20:47
合計ジャッジ時間 5,718 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 20 ms
4,380 KB
testcase_11 AC 22 ms
4,380 KB
testcase_12 AC 37 ms
4,628 KB
testcase_13 AC 34 ms
4,492 KB
testcase_14 AC 35 ms
4,612 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 WA -
testcase_30 AC 30 ms
7,028 KB
testcase_31 AC 58 ms
14,772 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
    }
}
0