Problem:


Score : 400 points

Problem Statement

We have N integers. The i-th integer is A_i.

Find \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

2 \leq N \leq 3 \times 10^5

0 \leq A_i < 2^{60}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the value \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).

Sample Input 1

3
1 2 3

Sample Output 1

6

We have (1\mbox{ XOR } 2)+(1\mbox{ XOR } 3)+(2\mbox{ XOR } 3)=3+2+1=6.

Sample Input 2

10
3 1 4 1 5 9 2 6 5 3

Sample Output 2

237

Sample Input 3

10
3 14 159 2653 58979 323846 2643383 27950288 419716939 9375105820

Sample Output 3

103715602

Print the sum modulo (10^9+7).



Sample input:
3
1 2 3


Sample output:
6

Submission status: Accepted

Solution code:
n=int(input())
a = list(map(int, input().split()))
b=[0]*60
ans=0
count=0
mod=10**9+7

for i in range(60):
    for j in range(n):
        if a[j]>>i&1==1:
            count+=1

    b[i]=count
    count=0

for i in range(60):
    ans+=2**i*(b[i]*(n-b[i]))
    ans%=mod


print(ans)

Submission status: Accepted

Solution code:
import sys, math
from collections import defaultdict
from copy import deepcopy
import itertools
input = sys.stdin.readline
# 文字列の場合は普通のinput()を使うか下を使う
# input = sys.stdin.readline().rstrip()

N = int(input())
A = list(map(int, input().split()))
for i, a in enumerate(A):
    a = bin(a)[2:]
    l = len(a)
    A[i] = '0' * (60 - l) + a
mod = 10**9 + 7

ans = 0
for i in range(60):
    num_0 = 0
    num_1 = 0
    for a in A:
        if a[-i-1] == '1':
            num_1 += 1
        else:
            num_0 += 1
    ans += (num_0 * num_1) * (2 ** i)
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array(input().split(),np.int64)
MOD = 10 ** 9 + 7

ans = 0
for i in range(60):
    c = np.count_nonzero((A>>i)&1)
    ans += (2 ** i) * c * (N - c)

print(ans%MOD)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array([int(x) for x in input().split()])
mod = 10**9+7
ans = 0

for i in range(60):
    c = np.count_nonzero((A>>i)&1)
    ans += (2 ** i) * c * (N - c)

print(ans%mod)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=0
mod=10**9+7
for i in range(61):
  a=sum([1 for j in A if j>>i & 1])
  ans=ans+a*(n-a)*(2**i)
  ans=ans%mod
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
a.sort()
ln = len(bin(a[-1])) - 2
bit = [[0,0] for i in range(ln)]
res = 0
for i in a:
    for j in range(ln):
        tmp = 0
        if (i>>j) % 2:
            tmp += 1
        bit[j][tmp] += 1
for i,j in enumerate(bit):
    c = pow(2,i)
    res += j[0] * j[1] * c
    res %= mod
print(res)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
a.sort()
ln = len(bin(a[-1])) - 2
bit = [defaultdict(int) for i in range(ln)]
res = 0
for i in a:
    tmp = bin(i)
    for _,j in enumerate(tmp[:1:-1]):
        # print(_,j)
        bit[_][j] += 1
# pprint.pprint(bit)
for i,j in enumerate(bit):
    x = j['0']
    y = j['1']
    if x+y < n:
        x = n - y
    # print(i,x,y)
    c = pow(2,i)
    res += x * y * c
    res %= mod
print(res)

Submission status: Accepted

Solution code:
N = int(input())
from collections import defaultdict
d = defaultdict(int)
for i in map(int,input().split()):
  for idx,j in enumerate(reversed('{:b}'.format(i))):
    if j == "1":
      d[idx]+=1

m = max( set(d.keys())|set([0]))
print(int(sum([((2**i)* (N-d[i])*d[i])%(1e9+7) for i in range(m+1)])%(1e9+7)))

Submission status: Accepted

Solution code:
#与えられる変数
N=int(input())
A=list(map(int,input().split()))
mod=10**9+7
ans=0

#Aの各要素の2進数表記した時のi番目の数について見ていく（Aの要素は最大でも2の60乗だから0≦i≦60）
#XORは2進数表記した時の2つの数が違うとき,その桁の数を1とする
#2進数表記したi番目の数が異なる場合の数がその桁での和として表現できる
#その場合の数は(0の個数)×(1の個数)で表現できる
for i in range(61):
    yes=sum([1 for a in A if (a>>i)&1])
    no=N-yes
    ans+=(1<<i)*yes*no

ans%=mod
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
ans=0
mod=10**9+7
for i in range(64):
    num_1=0
    num_0=0
    for ai in a:
        if (ai >> i) & 1:
            num_1+=1
        else:
            num_0+=1
    ans+=num_0*num_1*(1 << i)
print(ans%mod)

Submission status: Accepted

Solution code:
#import heapq
#import itertools
#import math
import numpy as np
#import sys

N = int(input())
A = list(map(int, input().split()))
A = np.array(A)

mod = 10 ** 9 + 7
s = 0

for i in range(60):
    #print(A)
    #print(A & 1)
    c = np.count_nonzero(A & 1) # 最下位のbitで0じゃないものの個数
    s += 2 ** i * c * (N - c) # https://qiita.com/ZhangChaoran/items/0d59eb5c95cd9414801d
    #print(c)
    A >>= 1
print(s % mod)


# N = int(input())
# #S = [input() for _ in range(H)]
# # T = [list(map(int,input().split())) for _ in range(N)]
# # sys.exit()
# #print(A)
# ans = 0
# for i in range(1,N) :
#     for j in range(i+1, N+1) :
#         #print(i,j)
#         ans += A[i - 1] ^ A[j - 1]

# # TLE orz
# print(ans % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]

# level_count = dict()
# for a in A:
#     bin_a = bin(a)
#     for i in range(len(bin_a[2:])):
#         if int(bin_a[-i-1]) == 1:
#             try:
#                 level_count[i] += 1
#             except:
#                 level_count[i] = 1
# 
# sum_ = 0
# for level, count in level_count.items():
#     sum_ += 2**level * count * (N - count)
# print(sum_ % (10**9 + 7))

import numpy as np

A = np.array(A)
sum_ = 0
for i in range(len(bin(np.max(A))) - 2):
    count = np.count_nonzero(A & 1)
    sum_ += 2**i * count * (N - count)
    A >>= 1
print(sum_ % (10**9 + 7))

Submission status: Accepted

Solution code:
import math
import collections
import itertools
import sys
import bisect
from heapq import heappop,heappush,heapify
sys.setrecursionlimit(10**6)
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

n = INT()
a = MAP()

m = 1
ans=0
for _ in range(60):
    bit = sum(i & m for i in a)
    bit //= m
    non_bit = n-bit
    ans = (ans+bit*non_bit*m)%MOD
    m <<= 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
mod=10**9+7
import numpy as np
a=np.array(a)
md = len(bin(max(a)))
ans = 0
for d in range(md):
    cnt1 = np.count_nonzero((a>>d)&1)
    ans += cnt1 * (n-cnt1) * (1<<d)
    ans %= mod

print(ans%mod)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
mod = 10**9+7;ans=0
for i in range(60):
  cnt=sum([1 for a in A if a>>i&1]) 
  cnt=cnt*(n-cnt)%mod
  cnt=2**i*cnt%mod
  ans+=cnt
print(ans%mod)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 0

for i in range(60):
    zeroCnt = 0
    oneCnt = 0

    for j in range(n):
        if (a[j] >> i) & 1 == 1:
            oneCnt += 1
        else:
            zeroCnt += 1
    ans += zeroCnt * oneCnt * pow(2, i, mod)
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
import numpy as np


def solve():
    MOD = 10**9+7
    N = int(input())
    a = np.array(input().split(), int)

    ans = 0
    for i in range(60):
        ones = np.count_nonzero((a >> i) & 1)
        zeros = N - ones
        ans += zeros * ones * 2 ** i
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

# input
n = int(input())
alist = list(map(int, input().split()))

# process
result = 0
for bit in range(60):
    m = 1 << bit
    c = sum(a & m for a in alist) >> bit
    result += (c * (n - c)) << bit
    result %= 10**9+7

# output
print(result)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
mod=10**9+7
for j in range(60):
    cnt1=sum([1 for i in a if i>>j&1])
    cnt0=n-cnt1
    ans+=(cnt1*cnt0*2**j)
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
mod = int(1e9+7)
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(60):
    a = 0
    b = 0
    for j in range(N):
        if A[j] % 2:
            a += 1
        else:
            b += 1
        A[j] //= 2
    ans += a*b*pow(2, i, mod)
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
MOD = int(1e9+7)


def div(a, b):
    return a * pow(b, MOD-2, MOD) % MOD


def main():
    N = int(input())
    A = list(map(int, input().split()))
    M = 60
    bitcnt = [0] * M
    for a in A:
        for i in range(M):
            if a & (1 << i):
                bitcnt[i] += 1
    ans = 0
    for a in A:
        for i in range(M):
            x = 1 << i
            if a & x:
                ans = (ans + x * (N - bitcnt[i])) % MOD
            else:
                ans = (ans + x * bitcnt[i]) % MOD
    ans = div(ans, 2)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a=list(map(int,input().split()))
ma=len(bin(max(a)))-2
b=np.array(a)
mod=10**9+7
ans=0
for i in range(ma):
  one=np.count_nonzero((b&1)==1)
  zero=n-one
  ans+=(one*zero*(2**i))%mod
  b=b//2
print(ans%mod)

Submission status: Accepted

Solution code:
#ABC147_D
#N個の数字から2個選ぶの全組み合わせのXORを足してmodする問題

import numpy as np

N = int(input())
arr = np.array([int(i) for i in input().split()])
ans = 0
mod=10**9+7


for i  in range(60):
    c1=np.count_nonzero(arr&1)#arrの最下位bitの論理和。arrの最下位bitが1の数をカウント
    c0=N-c1#arrの最下位bitが0の数
    ans+=2**i*c1*c0#c1個の中から1つ選ぶ組み合わせとc0個の中から1つ選ぶ組み合わせの掛け算が、該当bitがXOR＝１となる組み合わせの数
    arr>>=1#最下位bitを変更
print(ans%mod)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
A = LI()
ans = 0
for i in range(61):
    one_cnt = [a >> i & 1 for a in A].count(1)
    zero_cnt = n - one_cnt
    ans = (ans + pow(2, i, mod) * one_cnt * zero_cnt) % mod


print(ans)

Submission status: Accepted

Solution code:
# 全ての数字に対してn桁目のbitが立っているかを調べる
N = int(input())
A = list(map(int, input().split()))

ans = 0
mod = 10**9 + 7
for i in range(61):
    # n桁目のbitが立っているかを確認する
    bit = 1 << i
    count = 0
    for a in A:
        if bit & a > 0:
            count += 1
    ans += 2**i * (count) * (N-count) % mod
print(ans % mod)

Submission status: Accepted

Solution code:
n = int(input())

ass = list(map(int,input().split()))

ans = 0
for i in range(60):
    k = 1 << i
    i_1 = sum([k & a for a in ass]) >> i
    ans += i_1 * (n - i_1) << i
    ans = ans % ((10**9) + 7)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
 
MOD = 10 ** 9 + 7
 
N = int(input())
A = np.array(list(map(int, input().split())),np.int64)
 
answer = 0
for n in range(63):
    B = (A >> n) & 1
    x = np.count_nonzero(B)
    y = N - x
    x *= y
    for _ in range(n):
        x *= 2
        x %= MOD
    answer += x
answer %= MOD
print(answer)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
result = 0
for i in range(60):
    on = sum([(1 << i) & a for a in A]) >> i
    result += (on * (N - on)) << i
    result %= ((10**9) + 7)
 
print(result)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
each_bit_sum = [{"0": 0, "1": 0} for _ in range(62)]
result = 0
for a in A:
    for i, b in enumerate(format(a, "b").zfill(62)[::-1]):
        each_bit_sum[i][b] += 1
 
for i, b in enumerate(each_bit_sum):
    result += ((b["0"] * b["1"]) * (2**i)) % ((10**9) + 7)
 
print(result % ((10**9) + 7))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]

ans = 0
MOD = 10**9+7
for i in range(60):
    b = 1 << i
    cnt_1 = sum([a & b for a in A]) >> i
    cnt_0 = N - cnt_1
    b_sum = cnt_1 * cnt_0
    ans += (b_sum << i) % MOD
print(ans%MOD)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

result = 0
for i in range(60):
    target = 1 << i
    on = sum([target & a for a in A]) >> i
    result += (on * (N - on)) << i
    result %= ((10**9) + 7)

print(result)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
mod = 10**9+7
array = [0] * 60
for a in A:
    for j in range(len(bin(a))-2):
        array[j] += (a >> j) & 1
ans = 0
for i in range(60):
    ans += array[i] * (n-array[i]) * 2**i
print(ans % mod)

Submission status: Accepted

Solution code:
m = 10**9 + 7
n = int(input())
seq = [bin(int(x))[2:].zfill(61) for x in input().split()]
cnt = [[0,0] for i in range(61)]
for s in seq:
    for i in range(len(s)):
        if s[i] == '0':
            cnt[i][0] += 1
        else:
            cnt[i][1] += 1
ans = 0
for i in range(61):
  ans += cnt[-i-1][0]*cnt[-i-1][1]*(2**i)
  ans = ans % m
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.fromstring(input(), dtype = np.int64, sep = ' ')

result = 0
for bit in range(60):
    c = int((A & 1).sum())
    A >>= 1
    result = (result + c * (N - c) * (1 << bit)) % 1000000007
print(result)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.fromiter(map(int, input().split()), np.int64)

result = 0
for bit in range(60):
  c = int((A & 1).sum())
  A >>= 1
  result = (result + c * (N - c) * (1 << bit)) % 1000000007
print(result)

Submission status: Accepted

Solution code:
import logging
import numpy as np


logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)

Mod = 10 ** 9 + 7

def main():
    N = int(input())
    arr_1 = np.array(list(map(int, input().split())))
    logging.info("Hello!")

    ans = 0

    for i in range(64):
        num_of_1 = np.count_nonzero(arr_1 & 1)
        ans += (2 ** i) * (num_of_1 * (N - num_of_1))
        arr_1 >>= 1
        
    print(ans % Mod)
    
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

result = 0
for bit in range(60):
    m = 1 << bit
    c = sum(a & m for a in A) >> bit
    result += (c * (N - c)) << bit
    result %= 1000000007
print(result)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

result = 0
for bit in range(60):
    m = 1 << bit
    c = sum(a & m for a in A)
    c >>= bit
    result += (c * (N - c)) << bit
    result %= 1000000007
print(result)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

result = 0
for bit in range(60):
    m = 1 << bit
    c = sum((a & m for a in A))
    c >>= bit
    result += (c * (N - c)) << bit
    result %= 1000000007
print(result)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

result = 0
for bit in range(60):
    m = 1 << bit
    bits = sum((a & m for a in A))
    bits >>= bit
    result += (bits * (N -  bits)) << bit 
    result %= 1000000007
print(result)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
import numpy as np
a=np.array(a)
mod = 10**9+7
md = len(bin(max(a)))
ans = 0
for d in range(md):
    cnt1 = np.count_nonzero((a>>d)&1)# a内の全ての要素の d bit目の1の個数を数える
    ans += cnt1 * (n-cnt1) * (1<<d)          # 求めるべきは (0の個数)*(1の個数)をd bit目にそのまま収めたものを足し合わせたもの (?)
    ans %= mod                               # mod 取っておく
    
print(ans%mod)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
mod=10**9+7
for i in range(62):
	cnt=0
	for j in range(n):
		if (a[j]>>i)&1:
			cnt+=1
	ans += cnt*(n-cnt)%mod*pow(2,i,mod)%mod
	ans%=mod
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.fromiter(map(int, input().split()), np.int64)

result = 0
for b in range(60):
  bs = int(((A >> b) & 1).sum())
  result = (result + bs * (N - bs) * (1 << b)) % 1000000007
print(result)

Submission status: Accepted

Solution code:
mod = 10**9+7
from collections import defaultdict
N = int(input())
A = list(map(int,input().split()))
d = defaultdict(lambda:0)
ans = 0
for a in A:
    a = bin(a)[2:]
    for i in range(len(a)):
        if a[-i-1]=='1':
            d[i] += 1

for i in range(60):
    ans += pow(2,i,mod)*d[i]*(N-d[i])

ans %= mod
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array(list(map(int, input().split())))

result = 0
for b in range(60):
  bs = int(((A >> b) & 1).sum())
  result = (result + bs * (N - bs) * (1 << b)) % 1000000007
print(result)

Submission status: Accepted

Solution code:
import numpy as np
M = 10**9+7
N = int(input())
A = np.array(list(map(int,input().split())),dtype='int64')
ans = 0

maxA = np.amax(A)
K = len(str(bin(maxA)))-2

for k in range(K):
    N1 = 0
    N1 = np.sum((A >> k) & 1)
    N0 = N-N1
    ans += (((1<<k)%M) *N0%M) *N1%M
    ans %= M

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = str(input()).split(' ')
a_s = [int(a) for a in As]

import numpy as np
a_s = np.array(a_s)
sum = 0
index = 0
for n in range(60):
  b = (a_s >> n) & 1
  x = np.count_nonzero(b)
  y = N - x
  ans = x * y
  for _ in range(n):
    ans *= 2
  sum += ans
  
print(sum%(10**9+7))

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))

ans = 0
m = 1
for i in range(60):
    bits = sum((a & m for a in A))
    bits //= m
    ans += (bits * (N - bits) * m)
    ans %= MOD
    m <<= 1
print(ans % MOD)

Submission status: Accepted

Solution code:
M = 10**9+7
N = int(input())
A = list(map(int,input().split()))

ans = 0
for k in range(61):
    N1 = 0
    N1 = sum([1 for a in A if ( (a >> k) & 1)])
    ans += (((1<<k)) *(N-N1) *N1)
    ans %= M    

print(ans)

Submission status: Accepted

Solution code:
#input
N = int(input())
A = list(map(int, input().split()))

#output
import numpy as np
dp = np.array(A) 

answer = 0
for i in range(60):
    a = np.count_nonzero(dp & 1)
    answer += 2**i*a*(N-a)
    dp >>= 1

print(answer % (10**9+7))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7

ctr = [0] * 60
for a in A:
    for i in range(60):
        if (a>>i) & 1:
            ctr[i] += 1

ans = 0
for d, num1 in enumerate(ctr):
    num0 = n - num1
    ans += num0 * num1 * 2**d
    ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
import numpy as np
n=int(input())
a=np.array([int(j) for j in input().split()],dtype=np.int64)
ans=0
mod=10**9+7
for i in range(61):
    b=(a>>i)&1
    c=np.count_nonzero(b)
    ans+=(2**i)*c*(n-c)
    ans%=mod
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
A = np.array(list(map(int,input().split())))
mod = 10**9+7

ans = 0
for i in range(len(bin(max(A))[2:])):
	a = (A>>i)&1
	a = np.count_nonzero(a)
	b = n-a
	a *= b
	a = a<<i%mod
	ans = (ans+a)%mod
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

mod = 10 ** 9 + 7
n = int(input())
a = list(map(lambda x: format(int(x), "b").zfill(60), input().split()))

ans = 0
for i in range(60):
    cnt = 0
    for j in a:
        if j[- i - 1] == "1":
            cnt += 1
    tmp = cnt * (n - cnt)
    for _ in range(i):
        tmp = tmp * 2 % mod
    ans += tmp
print(ans % mod)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

mod = 10 ** 9 + 7
n = int(input())
a = list(map(lambda x: format(int(x), "b").zfill(60), input().split()))

ans = 0
for i in range(60):
    cnt = 0
    for j in a:
        if j[- i - 1] == "1":
            cnt += 1
    tmp = cnt * (n - cnt)
    for _ in range(i):
        tmp = tmp * 2 % mod
    ans += tmp
print(ans % mod)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))

mod = 10**9 + 7
total = 0
for i in range(60):
  cnt = sum([1 for a in A if (a>>i & 1)])
  total += cnt * (n-cnt) * (1 << i)
  total %= mod
print(total)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time
from fractions import gcd
#import random


def I(): return int(input())


def MI(): return map(int, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['No', 'Yes']
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


show_flg = True
show_flg = False

# xor
# n + i = n ^ i implies n & i = 0


def sumXOR(arr,  n):
    _sum = 0
    for i in range(0, 64):
        zc = 0
        oc = 0

        # Individual sum at each bit position
        idsum = 0
        for j in range(0, n):
            if (arr[j] % 2 == 0):
                zc = zc + 1
            else:
                oc = oc + 1
            arr[j] = arr[j] >> 1

        # calculating individual bit sum
        idsum = oc * zc * (1 << i)
        print(zc, oc, idsum)

        # final sum
        _sum = _sum + idsum
        _sum %= MOD

    return _sum


def main():
    N = I()
    A = list(MI())
    BIT = 61
    C = [[0] * BIT for i in range(N+1)]
    pows = [pow(2, i, MOD) for i in range(BIT)]
    ans = 0

    for i in range(N):
        bit = 1
        for j in range(BIT):
            if A[i] & bit:
                C[i+1][j] = C[i][j] + 1
            else:
                C[i+1][j] = C[i][j]
            bit = bit << 1

    for i in range(N):
        bit = 1
        for j in range(BIT):
            oc = C[N][j] - C[i][j]
            # print('b', ans, i, j, bit, oc)
            if A[i] & bit:
                zc = N - i - oc
                ans += pows[j] * zc
            elif oc != 0:
                ans += pows[j] * oc
            ans %= MOD
            bit = bit << 1
            # print('a', ans, i, j, bit, oc)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# from heapq import heappush, heappop, heapify
# from collections import deque, defaultdict, Counter
# import itertools
# from itertools import permutations, combinations, accumulate
import sys
# import bisect
# import string
# import math
# import time
# from fractions import gcd
#import random


def I(): return int(input())


def MI(): return map(int, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


# YN = ['No', 'Yes']
MOD = 10**9+7
# inf = float('inf')
# IINF = 10**10
# l_alp = string.ascii_lowercase
# u_alp = string.ascii_uppercase
# ts = time.time()
# sys.setrecursionlimit(10**6)
# nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


# show_flg = True
show_flg = False

# xor
# n + i = n ^ i implies n & i = 0


# def sumXOR(arr,  n):
#     _sum = 0
#     for i in range(0, 64):
#         zc = 0
#         oc = 0

#         # Individual sum at each bit position
#         idsum = 0
#         for j in range(0, n):
#             if (arr[j] % 2 == 0):
#                 zc = zc + 1
#             else:
#                 oc = oc + 1
#             arr[j] = arr[j] >> 1

#         # calculating individual bit sum
#         idsum = oc * zc * (1 << i)
#         print(zc, oc, idsum)

#         # final sum
#         _sum = _sum + idsum
#         _sum %= MOD

#     return _sum


def main():
    N = I()
    A = list(MI())
    BIT = 61
    C = [[0] * BIT for i in range(N+1)]
    pows = [pow(2, i, MOD) for i in range(BIT)]
    ans = 0

    for i in range(N):
        bit = 1
        for j in range(BIT):
            if A[i] & bit:
                C[i+1][j] = C[i][j] + 1
            else:
                C[i+1][j] = C[i][j]
            bit = bit << 1

    for i in range(N):
        bit = 1
        for j in range(BIT):
            oc = C[N][j] - C[i][j]
            # print('b', ans, i, j, bit, oc)
            if A[i] & bit:
                zc = N - i - oc
                ans += pows[j] * zc
            elif oc != 0:
                ans += pows[j] * oc
            ans %= MOD
            bit = bit << 1
            # print('a', ans, i, j, bit, oc)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def sol():
    mod=1000000007
    ans=0
    n=int(input())
    a=[int(i) for i in input().split()]
    x=[[0]*61 for i in range(n+1)]
    l=[pow(2,i,mod) for i in range(61)]
 
    for i in range(n):
        d=a[i]
        cnt=0
        for j in range(61):
            if d&1:
                x[i+1][cnt]=x[i][cnt]+1
            else:
                x[i+1][cnt]=x[i][cnt]
            cnt+=1
            d=d>>1
 
    for i in range(1,n):
        for j in range(61):
            p=x[i][j]-x[i-1][j]
            if p:
                ans+=l[j]*((n-i)-(x[-1][j]-x[i][j]))
            else:
                ans+=l[j]*(x[-1][j]-x[i][j])
            ans%=mod
    print(ans)
 
if __name__=="__main__":
    sol()

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
MOD = 10**9+7
N = int(input())
A = inpl()
K = len(bin(max(A)))-2
unit = 1
ans = 0
for k in range(K):
    c = 0
    for n in range(N):
        c += A[n] % 2
        A[n] //= 2
    ans += c*(N-c)*unit
    ans %= MOD
    unit = unit * 2 % MOD
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
mod = 10 ** 9 + 7
ans = 0
m = 1
for _ in range(60):
    bit = sum(a & m for a in x) #フラグが立ってる桁を合計
    bit //= m #1の個数を数えるためにmで割る
    non_bit = n - bit #0の個数を出す
    ans = (ans + bit * non_bit * m) % mod #桁の合計出す
    m <<= 1 #桁を1増やす
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    import numpy as np

    n = int(input())
    # 通常のリストを生成する感覚で使える
    a = np.array([int(ai) for ai in input().split()])
    mod = 10 ** 9 + 7
    ans = 0

    # See:
    # https://img.atcoder.jp/abc147/editorial.pdf
    # KeyInsight:
    # XORを取る操作で各ビットは干渉しないため，それぞれ独立として扱ってよい
    # XORが1となるのは，Ai != Aj，つまり0と1の組み合わせを求めることと同じ

    # numpyを使うと実装が楽になる
    # See:
    # https://qiita.com/ZhangChaoran/items/0d59eb5c95cd9414801d
    for i in range(60):
        # a & 1で最下位のbitが0/1を判定している

        # 0でない個数を数える
        # See:
        # https://docs.scipy.org/doc/numpy/reference/generated/numpy.count_nonzero.html
        count = np.count_nonzero(a & 1)
        # 下からiビット目: bitが0の個数×同1の個数
        ans += (2 ** i) * (n - count) * count
        # b ビット右シフト
        # See:
        # http://www.tohoho-web.com/python/operators.html
        a >>= 1

    print(ans % mod)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    a_list = list(map(int, input().split()))
    mod = 10 ** 9 + 7

    t = [0]*60

    for a in a_list:
        for count, i in enumerate(reversed(format(a, "b"))):
            if i == "1":
                t[count] += 1

    sum = 0
    for i, count in enumerate(t):
        sum += 2 ** i * count * (N - count)
    print(sum % mod)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
MOD = 10**9 + 7

n = int(input())

A = list(map(int, input().split()))
A = [format(a, 'b').zfill(60) for a in A]

ans = 0
for i in range(60):
    lst = [a[i] for a in A]
    num_1 = lst.count('1')
    num_0 = lst.count('0')

    ans = (ans + (2**(59 - i) * (num_0 * num_1)) % MOD) % MOD

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
A = np.array([int(i) for i in input().split()])
ans = 0
#print(A & 1)

# A_iは60ビットまで与えられる.
for i in range(60):
    #A&1は配列Aを1ビット目を表示する: [1,2,3]→[1,0,1]
    c = np.count_nonzero(A & 1) #配列Aの1ビット目が非ゼロのものをカウント
    #cは桁数nを超えないのでn-c>=0
    # c*(n-c)は(0の個数)*(1の個数)を表す.
    ans += 2**i*c*(n-c) #bit毎に計算したのち2**iをかけて10進数に戻す.
    # Aを1ビット右シフト
    A >>= 1 #一つ上の桁数でも同様. 大きな位では0なので値として出てこない.
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    import numpy as np

    n = int(input())
    a = np.array([int(ai) for ai in input().split()])
    mod = 10 ** 9 + 7
    ans = 0

    # KeyInsight:
    # XORを取る操作で各ビットは干渉しないため，それぞれ独立として扱ってよい
    # XORが1となるのは，Ai != Aj，つまり0と1の組み合わせを求めることと同じ

    # See:
    # https://qiita.com/ZhangChaoran/items/0d59eb5c95cd9414801d
    for i in range(60):
        count = np.count_nonzero(a & 1)
        ans += (2 ** i) * (n - count) * count
        a >>= 1

    print(ans % mod)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
amax=max(a)
m=len( bin(amax)[2:] )
bin_a = [bin(aa)[2:].zfill(m) for aa in a]

ans=0
mod=10**9+7
for i in range(m):
    sum0 = sum( bi[-1-i]=="0" for bi in bin_a )
    sum1 = n-sum0
    ans += sum0*sum1*2**i % mod
print(ans%mod)