結果

問題 No.1240 Or Sum of Xor Pair
ユーザー MisterMister
提出日時 2020-09-26 14:49:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 282 ms / 2,000 ms
コード長 2,024 bytes
コンパイル時間 891 ms
コンパイル使用メモリ 76,764 KB
実行使用メモリ 6,108 KB
最終ジャッジ日時 2023-09-11 11:33:37
合計ジャッジ時間 11,664 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 239 ms
5,468 KB
testcase_01 AC 238 ms
5,416 KB
testcase_02 AC 240 ms
5,348 KB
testcase_03 AC 243 ms
5,396 KB
testcase_04 AC 243 ms
5,504 KB
testcase_05 AC 245 ms
5,392 KB
testcase_06 AC 242 ms
5,424 KB
testcase_07 AC 242 ms
5,496 KB
testcase_08 AC 243 ms
5,556 KB
testcase_09 AC 241 ms
5,620 KB
testcase_10 AC 245 ms
5,564 KB
testcase_11 AC 247 ms
5,520 KB
testcase_12 AC 249 ms
5,380 KB
testcase_13 AC 249 ms
5,464 KB
testcase_14 AC 251 ms
5,388 KB
testcase_15 AC 280 ms
6,100 KB
testcase_16 AC 280 ms
5,908 KB
testcase_17 AC 281 ms
5,860 KB
testcase_18 AC 280 ms
5,892 KB
testcase_19 AC 280 ms
5,904 KB
testcase_20 AC 281 ms
5,824 KB
testcase_21 AC 279 ms
5,848 KB
testcase_22 AC 281 ms
5,904 KB
testcase_23 AC 279 ms
5,860 KB
testcase_24 AC 282 ms
5,896 KB
testcase_25 AC 281 ms
6,108 KB
testcase_26 AC 279 ms
5,972 KB
testcase_27 AC 239 ms
5,436 KB
testcase_28 AC 239 ms
5,688 KB
testcase_29 AC 264 ms
5,908 KB
testcase_30 AC 253 ms
5,428 KB
testcase_31 AC 252 ms
5,376 KB
testcase_32 AC 262 ms
5,912 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 = tot;

        {
            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 * num;
    }

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

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

    solve();

    return 0;
}
0