aacord’s memo

abcを中心にpythonで解いた問題のメモ、整理をしています。緑になった。

abc 147 d 'XOR sun 4' (python)

maspy さんの解説をひたすら読んだ
[AtCoder 参加感想] 2019/12/08:ABC 147 | maspyのHP
XOR関連の共通した着目点は、各桁ごとのbit 演算は独立して計算できるということ
i を2進数表示したときの n 桁目は ( i >> n ) & 1 で表せる
ある桁のXORのn個の総和は結局、(1である個数)*(0である個数)でかける

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
import collections
mod = 10 ** 9 + 7
 
N = int(readline())
A = [int(i) for i in readline().split()]
 
answer = 0
for n in range(62):
    B = [(i >> n) & 1 for i in A]
    C = collections.Counter(B)
    x = C[1]
    y = N - x
    x *= y
    for _ in range(n):
        x *= 2
        x %= mod
    answer += x
    answer %= mod
print(answer)