結果

問題 No.1240 Or Sum of Xor Pair
ユーザー MisterMister
提出日時 2020-09-26 14:52:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 2,024 bytes
コンパイル時間 678 ms
コンパイル使用メモリ 77,448 KB
実行使用メモリ 5,948 KB
最終ジャッジ日時 2023-09-11 11:39:54
合計ジャッジ時間 11,312 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 240 ms
5,420 KB
testcase_01 AC 239 ms
5,524 KB
testcase_02 AC 239 ms
5,476 KB
testcase_03 AC 244 ms
5,416 KB
testcase_04 AC 243 ms
5,420 KB
testcase_05 AC 241 ms
5,476 KB
testcase_06 AC 242 ms
5,492 KB
testcase_07 AC 242 ms
5,600 KB
testcase_08 AC 243 ms
5,420 KB
testcase_09 AC 241 ms
5,560 KB
testcase_10 AC 245 ms
5,480 KB
testcase_11 AC 247 ms
5,496 KB
testcase_12 AC 249 ms
5,404 KB
testcase_13 AC 249 ms
5,400 KB
testcase_14 AC 252 ms
5,396 KB
testcase_15 AC 282 ms
5,868 KB
testcase_16 AC 280 ms
5,928 KB
testcase_17 AC 282 ms
5,944 KB
testcase_18 AC 279 ms
5,876 KB
testcase_19 AC 281 ms
5,860 KB
testcase_20 AC 283 ms
5,872 KB
testcase_21 AC 280 ms
5,948 KB
testcase_22 AC 280 ms
5,868 KB
testcase_23 AC 281 ms
5,932 KB
testcase_24 AC 281 ms
5,884 KB
testcase_25 AC 281 ms
5,932 KB
testcase_26 AC 280 ms
5,928 KB
testcase_27 AC 239 ms
5,400 KB
testcase_28 AC 238 ms
5,420 KB
testcase_29 AC 264 ms
5,924 KB
testcase_30 AC 250 ms
5,456 KB
testcase_31 AC 252 ms
5,400 KB
testcase_32 AC 261 ms
5,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <numeric>
#include <vector>

using lint = long long;
constexpr int K = 18;

// 自身とのxor畳み込み
void xor_convolution(std::vector<lint>& xs) {
    for (int i = 1; i < (1 << K); i <<= 1) {
        for (int b = 0; b < (1 << K); ++b) {
            if (b & i) continue;
            auto l = xs[b],
                 r = xs[b | i];
            xs[b] = l + r;
            xs[b | i] = l - r;
        }
    }

    for (auto& x : xs) x *= x;

    for (int i = 1; i < (1 << K); i <<= 1) {
        for (int b = 0; b < (1 << K); ++b) {
            if (b & i) continue;
            auto l = xs[b],
                 r = xs[b | i];
            xs[b] = l + r;
            xs[b | i] = l - r;
        }
    }

    for (auto& x : xs) x >>= K;
}

void solve() {
    int n, a;
    std::cin >> n >> a;

    std::vector<int> xs(n);
    for (auto& x : xs) std::cin >> x;

    lint tot;  // 全体の個数
    {
        int sz = 0;
        std::vector<lint> cnt(1 << K, 0);
        for (auto x : xs) {
            ++cnt[x];
            ++sz;
        }

        xor_convolution(cnt);
        cnt[0] -= sz;                 // 0にi=jなるものが含まれることに注意
        for (auto& c : cnt) c >>= 1;  // i>jを除外
        tot = std::accumulate(cnt.begin(), cnt.begin() + a, 0LL);
    }

    lint ans = 0;
    for (int k = 1; k < (1 << K); k <<= 1) {
        // k桁目が0であるもののみの個数
        lint num;
        {
            int sz = 0;
            std::vector<lint> cnt(1 << K, 0);
            for (auto x : xs) {
                if (x & k) continue;
                ++cnt[x];
                ++sz;
            }

            xor_convolution(cnt);
            cnt[0] -= sz;
            for (auto& c : cnt) c >>= 1;
            num = std::accumulate(cnt.begin(), cnt.begin() + a, 0LL);
        }

        ans += k * (tot - num);
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0