結果

問題 No.1240 Or Sum of Xor Pair
ユーザー carrot46carrot46
提出日時 2020-09-28 11:30:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 491 ms / 2,000 ms
コード長 1,906 bytes
コンパイル時間 1,512 ms
コンパイル使用メモリ 169,136 KB
実行使用メモリ 15,220 KB
最終ジャッジ日時 2023-09-14 12:56:27
合計ジャッジ時間 18,818 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 419 ms
13,344 KB
testcase_01 AC 418 ms
13,444 KB
testcase_02 AC 417 ms
13,396 KB
testcase_03 AC 418 ms
13,336 KB
testcase_04 AC 424 ms
13,320 KB
testcase_05 AC 422 ms
13,536 KB
testcase_06 AC 420 ms
13,312 KB
testcase_07 AC 426 ms
13,360 KB
testcase_08 AC 425 ms
13,316 KB
testcase_09 AC 425 ms
13,484 KB
testcase_10 AC 433 ms
13,480 KB
testcase_11 AC 436 ms
13,484 KB
testcase_12 AC 437 ms
13,672 KB
testcase_13 AC 437 ms
13,592 KB
testcase_14 AC 441 ms
13,968 KB
testcase_15 AC 485 ms
14,892 KB
testcase_16 AC 487 ms
15,032 KB
testcase_17 AC 489 ms
15,040 KB
testcase_18 AC 491 ms
15,020 KB
testcase_19 AC 483 ms
14,948 KB
testcase_20 AC 488 ms
15,064 KB
testcase_21 AC 488 ms
14,856 KB
testcase_22 AC 486 ms
14,888 KB
testcase_23 AC 490 ms
14,848 KB
testcase_24 AC 484 ms
14,920 KB
testcase_25 AC 486 ms
14,952 KB
testcase_26 AC 487 ms
14,848 KB
testcase_27 AC 419 ms
13,344 KB
testcase_28 AC 417 ms
13,288 KB
testcase_29 AC 483 ms
15,000 KB
testcase_30 AC 447 ms
14,144 KB
testcase_31 AC 450 ms
14,144 KB
testcase_32 AC 482 ms
15,220 KB
権限があれば一括ダウンロードができます

ソースコード

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);

template <class T> void adamard(vector<T> &a, bool inv){
    int n = a.size();
    for(int i = 1; i < n; i <<= 1){
        int m = n - i;
        for(int j = 0; j < m; ++j){
            j += j&i;
            T temp = a[j|i];
            a[j|i] = a[j] - temp;
            a[j] += temp;
            if(inv){
                a[j] /= 2;
                a[j|i] /= 2;
            }
        }
    }
}

template <class T> vector<T> xor_convolution(vector<T> &a, vector<T> &b){
    //can_divide ? " / 2" : " * 2.inv()"
    int n(1);
    while(n < max((int)a.size(), (int)b.size())) n <<= 1;
    vector<T> a_cpy(n), b_cpy(n);
    copy(ALL(a), a_cpy.begin());
    copy(ALL(b), b_cpy.begin());
    adamard(a_cpy, false);
    adamard(b_cpy, false);
    rep(i, n) a_cpy[i] *= b_cpy[i];
    adamard(a_cpy, true);
    return a_cpy;
}

int main() {
    const int max_A = 1<<18;
    cin>>N>>K;
    vec a(N), cnt(max_A, 0), cnt_cnv;
    rep(i, N) {cin>>a[i]; ++cnt[a[i]];}
    cnt_cnv = xor_convolution(cnt, cnt);
    ll res(-accumulate(ALL(a), 0LL));
    for(int n = 1; n < max_A; n <<= 1){
        vec cnt_without_bitk(max_A, 0);
        int r = max_A - n;
        rep(i, r){
            i += i & n;
            cnt_without_bitk[i] = cnt[i];
        }
        vec cnt_without_bitk_cnv = xor_convolution(cnt_without_bitk, cnt_without_bitk);
        rep(i, K) res += (cnt_cnv[i] - cnt_without_bitk_cnv[i]) * n;
    }
    cout<<res / 2<<endl;
}
0