結果

問題 No.1240 Or Sum of Xor Pair
ユーザー 👑 tatyam
提出日時 2020-08-24 22:31:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 861 ms / 2,000 ms
コード長 1,211 bytes
コンパイル時間 341 ms
コンパイル使用メモリ 82,336 KB
実行使用メモリ 154,988 KB
最終ジャッジ日時 2024-06-22 07:09:17
合計ジャッジ時間 27,300 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

def fwht(a):
    n = len(a)
    j = 1
    while j < n:
        for i in range(0, n):
            if i & j == 0:
                a[i], a[i | j] = a[i] + a[i | j], a[i] - a[i | j]
        j <<= 1
    return a

def ifwht(a):
    n = len(a)
    j = 1
    while j < n:
        for i in range(0, n):
            if i & j == 0:
                a[i], a[i | j] = (a[i] + a[i | j]) >> 1, (a[i] - a[i | j]) >> 1
        j <<= 1
    return a

n, x = map(int, input().split())
assert 2 <= n <= 200000
assert 1 <= x <= 2**18
a = [*map(int, input().split())]
assert len(a) == n
for i in a:
    assert 0 <= i < 2**18

cnt = [0] * 2**18
for i in a:
    cnt[i] += 1

# XOR 畳み込み
fwht(cnt)
for i in range(2**18):
    cnt[i] **= 2
ifwht(cnt)

ans = 0
for i in range(x):
    ans += cnt[i] // 2 * i

# x OR y == x XOR y + x AND y より, 残りの x AND y を bit ごとに足す
for j in range(18):
    bit = 1 << j
    cnt = [0] * 2**18
    s = 0
    for i in a:
        if i & bit:
            cnt[i] += 1
            s -= 1

    # XOR 畳み込み
    fwht(cnt)
    for i in range(2**18):
        cnt[i] **= 2
    ifwht(cnt)

    s += sum(cnt[:x])
    ans += s // 2 * bit

print(ans)
0