結果

問題 No.1240 Or Sum of Xor Pair
ユーザー 👑 tatyamtatyam
提出日時 2020-08-24 22:31:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,002 ms / 2,000 ms
コード長 1,211 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 86,664 KB
実行使用メモリ 155,988 KB
最終ジャッジ日時 2023-09-04 07:53:24
合計ジャッジ時間 33,818 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 853 ms
115,640 KB
testcase_01 AC 854 ms
115,540 KB
testcase_02 AC 854 ms
115,396 KB
testcase_03 AC 904 ms
152,124 KB
testcase_04 AC 903 ms
147,444 KB
testcase_05 AC 892 ms
139,332 KB
testcase_06 AC 899 ms
139,840 KB
testcase_07 AC 897 ms
145,596 KB
testcase_08 AC 913 ms
147,752 KB
testcase_09 AC 892 ms
138,760 KB
testcase_10 AC 897 ms
143,920 KB
testcase_11 AC 914 ms
154,044 KB
testcase_12 AC 909 ms
137,884 KB
testcase_13 AC 926 ms
149,268 KB
testcase_14 AC 929 ms
137,484 KB
testcase_15 AC 991 ms
147,852 KB
testcase_16 AC 990 ms
144,208 KB
testcase_17 AC 1,000 ms
151,412 KB
testcase_18 AC 984 ms
140,300 KB
testcase_19 AC 997 ms
152,044 KB
testcase_20 AC 1,002 ms
154,944 KB
testcase_21 AC 976 ms
138,876 KB
testcase_22 AC 988 ms
147,520 KB
testcase_23 AC 1,000 ms
153,372 KB
testcase_24 AC 1,002 ms
153,552 KB
testcase_25 AC 993 ms
154,344 KB
testcase_26 AC 983 ms
144,836 KB
testcase_27 AC 854 ms
115,512 KB
testcase_28 AC 853 ms
115,356 KB
testcase_29 AC 953 ms
155,988 KB
testcase_30 AC 914 ms
137,684 KB
testcase_31 AC 894 ms
115,868 KB
testcase_32 AC 938 ms
130,356 KB
権限があれば一括ダウンロードができます

ソースコード

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