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:
import numpy as np

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

divider = 10**9+7

ans = 0
for shift in range(0, 60):
    one_num = np.count_nonzero(A & 1)
    ans += (2**shift)*one_num*(N-one_num)
    A >>= 1
print(ans%divider)

Submission status: Accepted

Solution code:
import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    ls = [0] * 65
    for i in range(65):
        for v in a:
            ls[i] += v >> i & 1
    t = 0
    for i in range(65):
        t += ls[i] * (n - ls[i]) * 2 ** i
        t %= mod
    print(t % mod)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

answer = 0
for i in range(60):
    n_ones = np.count_nonzero(A & 1)
    answer += 2 ** i * (n - n_ones) * n_ones
    A >>= 1

print(answer % MOD)

Submission status: Accepted

Solution code:
mod = 10**9 + 7

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

B = [0] * 60
C = [0] * 60

for a in A:
    for j in range(60):
        if (a >> j) & 1:
            B[j] += 1
        else:
            C[j] += 1

ans = 0

for j in range(60):
    ans += (1 << j) * B[j] * C[j]

ans %= mod
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())))
 
ans=0
 
for i in range(60):
    cnt=np.count_nonzero(A&1)
    ans +=2**i*cnt*(n-cnt)
    A >>=1
 
print(ans%mod)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np


N = int(input())
A = np.array([int(i) for i in input().split()])
maxA = max(A)

MOD = 10**9 + 7

answer = 0
# (i+1)桁目のとき、power_of_2 = 2^i
i = 0
power_of_2 = 1
while maxA:
    B = (A >> i) & 1
    # 1の個数
    count1 = np.count_nonzero(B)
    # 0の個数
    count0 = N - count1
    # XORが1となる個数
    count = count0 * count1
    answer = (answer + count * power_of_2) % MOD

    i += 1
    maxA >>= 1

    power_of_2 = (power_of_2 * 2) % MOD

print(answer)

Submission status: Accepted

Solution code:
import numpy as np
mod=10**9+7
n=int(input())
A=np.array(list(map(int,input().split())))

ans=0

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

print(ans%mod)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
arr = np.array(list(map(int, input().split())))

mod = 10 ** 9 + 7

ans = 0
for i in range(60):
    c1 = np.count_nonzero(arr & 1)
    ans += 2 ** i * c1 * (n - c1)
    arr >>= 1

print(ans%mod)

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())

MOD = 10 ** 9 + 7
ans = 0
for bit in range(60):
    ctr = [0, 0]
    for i in range(N):
        ctr[A[i] >> bit & 1] += 1
    ans += (1 << bit) * (ctr[0] * ctr[1])
    ans %= MOD
    
print(ans)

Submission status: Accepted

Solution code:
import math

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

ans = 0
fil = 1
for i in range(60):
    num1 = len([1 for j in range(N) if A[j] & fil])
    num0 = N - num1
    ans += (num1 * num0) * fil
    fil <<= 1

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

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
A=np.array(list(map(int,input().split())))
l=len(format(A.max(),'b'))
MOD=10**9+7

tei=1
ans=0
for i in range(l):
  B=(A >> i) & 1
  one=np.count_nonzero(B)
  zero=N-one
  ans+=tei*one*zero
  tei*=2
print(ans%MOD)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
n = int(input())
a = list(map(int, input().split()))
s = 0
m = 1
for i in range(60):
    c = sum(ai & m for ai in a)
    c //= m
    s = (s + m*c*(n-c))%MOD
    m <<= 1
print(s)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
a = np.array([int(x) for x in input().split()])
inf = 10**9+7

ans = 0
p = 1
for i in range(61):
    a, b = a//2, a%2
    key = int(b.sum())
    ans += p*(n-key)*key
    ans = ans%inf
    p *= 2
    p %= inf
print(ans)

Submission status: Accepted

Solution code:
N = 60
MOD = 10**9 + 7
n = int(input())
# c1 = [0 for _ in range(N)]
c1 = [0 for _ in range(N)]

a = list(map(int,input().split()))
for i in range(n):
    for j in range(N):
        if a[i] & (1 << j) != 0:
            c1[j] += 1
        # else:
        #     c1[j] += 1

ans = 0
for i in range(n-1):
    for j in range(N):
        if a[i] & (1 << j) != 0:
            c1[j] -=1
        # else:
        #     c0[j] -=1
    for j in range(N):
        if a[i] & (1 << j) != 0:
            ans += (1<<j) * (n-i-1-c1[j])
        else:
            ans += (1<<j) * c1[j]
            #ans += (1<<j) * c1[j]

print(ans % MOD)

Submission status: Accepted

Solution code:
import sys
lines = sys.stdin.readlines()
n = int(lines[0])
arr = list(map(int, lines[1].split(' ')))

maxi = 62
mod = int(1e9) + 7
ones = [0 for _ in range(maxi)]
for num in arr:
    for idx, val in enumerate(range(maxi)):
        ones[idx] += (num >> idx ) & 1

ans = 0
for idx, val in enumerate(ones):
    ans += ((pow(2, idx) % mod) * (((n - val)%mod) * (val%mod))) % mod

    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    MAX = 10**9+7

    n = int(input())
    a = tuple(map(int, input().split()))

    li = [0]*61
    for i in a:
        for j in range(61):
            li[j] += i&1
            i >>= 1
    ans = 0
    bit = 1
    for i in li:
        ans += bit * i * (n-i)
        bit <<= 1
    print(ans%MAX)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
f = [0 for i in range(64)]
n = int(input())
mod = int(1e9+7)
x = map(int, input().split())
for i in range(n):
    for num in x:
        for j in range(61):
            if num & (1<<j) != 0:
                f[j] += 1
ans = 0
for i in range(64):
    cur = (1<<i) % mod
    ans += cur * (f[i]) * (n-f[i])
    ans = ans % mod
print(int(ans))

Submission status: Accepted

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

n=int(input())
a=[int(i) for i in input().split()]
ans=0
for b in range(60)[::-1]:
  ans<<=1
  c=sum(a[i]>>b & 1 for i in range(n))
  ans = (ans + c*(n-c)) % mod
print(ans)

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:
def sol():
    mod=1000000007
    ans=0
    n=int(input())
    a=[int(i) for i in input().split()]
    x=[[0]*63 for i in range(n+1)]
    #l=[yy(2**i)%mod for i in range(63)]
    l=[pow(2,i,mod) for i in range(63)]

    for i in range(n):
        d=a[i]
        cnt=0
        for j in range(63):
            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(63):
            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:
import numpy as np
N = int(input())
A = np.array([int(_) for _ in input().split()], dtype=np.int64)
mod = 10**9 + 7
ans = 0
for b in range(60):
    B = (A >> b) & 1
    M = np.count_nonzero(B)
    ans += 2**b * M * (N - M)
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
def sol():
    mod=1000000007
    ans=0
    n=int(input())
    a=[int(i) for i in input().split()]
    x=[[0]*63 for i in range(n+1)]
    l=[(2**i)%mod for i in range(63)]

    for i in range(n):
        d=a[i]
        cnt=0
        for j in range(63):
            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(63):
            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:
n = int(input())
a = list(map(int,input().split()))

ans = 0
 
MOD = (10**9+7)
for i in range(62):
    c = 0
    for ai in a:
        if ai&(1<<i): c += 1
    
    r = c*(n-c)
    ans += (1<<i)*r
    ans %= MOD
 
 
print(ans)

Submission status: Accepted

Solution code:
mod=1000000007
ans=0
n=int(input())
a=[int(i) for i in input().split()]
x=[[0]*63 for i in range(n+1)]
l=[(2**i)%mod for i in range(63)]

for i in range(n):
    d=a[i]
    cnt=0
    for j in range(63):
        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(63):
        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)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
 
N=int(input())
An = list(map(int,input().split()))
MOD = 10**9+7

ans=0
for i in range(60):
  ones,zeros=0,0
  for A in An:
    if (A>>i)%2 == 1:
      ones+=1
    else:
      zeros+=1
  ans+=2**i * ones * zeros
  ans%=MOD

print(ans)

Submission status: Accepted

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

    MOD = 10 ** 9 + 7

    lst = [0] * 65
    for a in A:
        tmp = a
        for i in range(61):
            lst[i] += tmp % 2
            tmp //= 2
            if tmp == 0:
                break

    calc = [1]
    for i in range(65):
        calc.append((calc[-1] * 2) % MOD)

    ans = 0
    for a in A:
        tmp = a
        for i in range(61):
            n = tmp % 2
            tmp //= 2
            if n == 1:
                ans += (calc[i] * (N - lst[i])) % MOD
            else:
                ans += (calc[i] * lst[i]) % MOD
        ans %= MOD

    def modinv(a, m): #mod m でのaの逆元 a^{-1}を計算する
        b = m
        u = 1
        v = 0
        while b:
            t = a//b
            a -= t * b
            a, b = b, a
            u -= t * v
            u, v = v, u
        
        u %= m
        if u < 0:
            u += m
        return u

    # print (ans)
    ans *= modinv(2, MOD)
    print (ans % MOD)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

MOD = 10 ** 9 + 7

lst = [0] * 65
for a in A:
    tmp = a
    for i in range(61):
        lst[i] += tmp % 2
        tmp //= 2
        if tmp == 0:
            break

calc = [1]
for i in range(65):
    calc.append((calc[-1] * 2) % MOD)

ans = 0
for a in A:
    tmp = a
    for i in range(61):
        n = tmp % 2
        tmp //= 2
        if n == 1:
            ans += calc[i] * (N - lst[i])
        else:
            ans += calc[i] * lst[i]
    ans %= MOD

def modinv(a, m): #mod m でのaの逆元 a^{-1}を計算する
    b = m
    u = 1
    v = 0
    while b:
        t = a//b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    
    u %= m
    if u < 0:
        u += m
    return u

# print (ans)
ans *= modinv(2, MOD)
print (ans % MOD)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
MOD = (10**9) + 7


class modint():
    def __init__(self, value):
        self.value = value % MOD

    def __int__(self):
        return int(self.value)

    def __float__(self):
        return float(self.value)

    def __str__(self):
        return str(self.value)

    def __repr__(self):
        return str(self.value)

    def __add__(self, other):
        return (modint(self.value + other.value) if isinstance(other, modint)
                else modint(self.value + other))

    def __sub__(self, other):
        return (modint(self.value - other.value) if isinstance(other, modint)
                else modint(self.value - other))

    def __mul__(self, other):
        return (modint(self.value * other.value) if isinstance(other, modint)
                else modint(self.value * other))

    def __truediv__(self, other):
        return (modint(self.value * pow(other.value, MOD - 2, MOD))
                if isinstance(other, modint)
                else modint(self.value * pow(other, MOD - 2, MOD)))

    def __pow__(self, other):
        return (modint(pow(self.value, other.value, MOD))
                if isinstance(other, modint)
                else modint(pow(self.value, other, MOD)))

    def __eq__(self, other):
        return (self.value == other.value if isinstance(other, modint)
                else self.value == (other % MOD))

    def __ne__(self, other):
        return (self.value == other.value if isinstance(other, modint)
                else self.value == (other % MOD))

    def __radd__(self, other):
        return (modint(other.value + self.value) if isinstance(other, modint)
                else modint(other + self.value))

    def __rsub__(self, other):
        return (modint(other.value - self.value) if isinstance(other, modint)
                else modint(other - self.value))

    def __rmul__(self, other):
        return (modint(other.value * self.value) if isinstance(other, modint)
                else modint(other * self.value))

    def __rtruediv__(self, other):
        return (modint(other.value * pow(self.value, MOD - 2, MOD))
                if isinstance(other, modint)
                else modint(other * pow(self.value, MOD - 2, MOD)))

    def __rpow__(self, other):
        return (modint(pow(other.value, self.value, MOD))
                if isinstance(other, modint)
                else modint(pow(other, self.value, MOD)))

    def modinv(self):
        """
        self.valueのmod MOD上での乗法逆元を返す
        >>> MOD
        13
        >>> x = modint(4)
        >>> x.modinv
        <bound method modint.modinv of 4>
        >>> x.modinv()
        10
        """
        return modint(pow(self.value, MOD - 2, MOD))


def main():
    import sys
    input = sys.stdin.buffer.readline
    N = int(input())
    A = ["{:0>60b}".format(int(i)) for i in input().split()]
    ans = modint(0)
    for i in range(60):
        num_one = 0
        for a in A:
            if a[-(i+1)] == "1":
                num_one += 1
        ans += ((N-num_one)*num_one)*(2**i)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(62):
    c=0
    for ai in a:
        if ai&(1<<i):
            c+=1
    r=c*(n-c)
    ans+=(1<<i)*r
    ans%=(10**9+7)
print(ans)

Submission status: Accepted

Solution code:
MOD = (10**9) + 7


class modint():
    def __init__(self, value):
        self.value = value % MOD

    def __int__(self):
        return int(self.value)

    def __float__(self):
        return float(self.value)

    def __str__(self):
        return str(self.value)

    def __repr__(self):
        return str(self.value)

    def __add__(self, other):
        return (modint(self.value + other.value) if isinstance(other, modint)
                else modint(self.value + other))

    def __sub__(self, other):
        return (modint(self.value - other.value) if isinstance(other, modint)
                else modint(self.value - other))

    def __mul__(self, other):
        return (modint(self.value * other.value) if isinstance(other, modint)
                else modint(self.value * other))

    def __truediv__(self, other):
        return (modint(self.value * pow(other.value, MOD - 2, MOD))
                if isinstance(other, modint)
                else modint(self.value * pow(other, MOD - 2, MOD)))

    def __pow__(self, other):
        return (modint(pow(self.value, other.value, MOD))
                if isinstance(other, modint)
                else modint(pow(self.value, other, MOD)))

    def __eq__(self, other):
        return (self.value == other.value if isinstance(other, modint)
                else self.value == (other % MOD))

    def __ne__(self, other):
        return (self.value == other.value if isinstance(other, modint)
                else self.value == (other % MOD))

    def __radd__(self, other):
        return (modint(other.value + self.value) if isinstance(other, modint)
                else modint(other + self.value))

    def __rsub__(self, other):
        return (modint(other.value - self.value) if isinstance(other, modint)
                else modint(other - self.value))

    def __rmul__(self, other):
        return (modint(other.value * self.value) if isinstance(other, modint)
                else modint(other * self.value))

    def __rtruediv__(self, other):
        return (modint(other.value * pow(self.value, MOD - 2, MOD))
                if isinstance(other, modint)
                else modint(other * pow(self.value, MOD - 2, MOD)))

    def __rpow__(self, other):
        return (modint(pow(other.value, self.value, MOD))
                if isinstance(other, modint)
                else modint(pow(other, self.value, MOD)))

    def modinv(self):
        """
        self.valueのmod MOD上での乗法逆元を返す
        >>> MOD
        13
        >>> x = modint(4)
        >>> x.modinv
        <bound method modint.modinv of 4>
        >>> x.modinv()
        10
        """
        return modint(pow(self.value, MOD - 2, MOD))


def main():
    N = int(input())
    A = ["{:0>60b}".format(int(i)) for i in input().split()]
    ans = modint(0)
    for i in range(60):
        num_one = 0
        for a in A:
            if a[-(i+1)] == "1":
                num_one += 1
        ans += ((N-num_one)*num_one)*(2**i)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
for i in range(60):
  p = 1 << i
  num_one = len([1 for x in A if x & p])
  ans += p * num_one * (N - num_one)
print(ans % 1000000007)

Submission status: Accepted

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

ans = 0
for i in range(60):
  p = 1 << i
  num_one = len([1 for x in A if x & p])
  ans = ans + p * num_one * (N - num_one)
print(ans % 1000000007)

Submission status: Accepted

Solution code:
import numpy as np

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

DIV=10**9+7

ans=0
for i in range(60):
  bits=(A>>i)&1
  nonzero=np.count_nonzero(bits)
  zeros=N-nonzero
  pat=nonzero*zeros
  for p in range(i):
    pat*=2
  ans+=pat
  ans%=DIV
  
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
src = list(map(lambda x: str(bin(int(x)))[2:], input().split()))
c = Counter()
mod = 10**9 + 7
for num in src:
    l = len(num)
    num = num[::-1]
    for i in range(l):
        if num[i] == '1':
            c[2**i] += 1
ans = 0
for i in c:
    ans += i*(N-c[i])*c[i]
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

MOD = 10 ** 9 + 7

n, *a = map(int, sys.stdin.read().split())
a = np.array(a, dtype=np.int64)

def main():
    ans = 0
    for i in range(60):
        b = (a >> i) & 1
        x = np.count_nonzero(b)
        y = n - x
        x *= y
        x *= pow(2, i, MOD)
        ans += x
    ans %= MOD
    return ans

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

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

import numpy as np

mod = 10**9+7

n = int(input())
*a, = map(int, input().split())
a = np.array(a)

ans = 0
for i in range(60):
    bits = np.count_nonzero(a&1)
    ans += 2**i*bits*(n-bits)
    a >>= 1
    
print(ans%mod)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
arr = np.array([int(i) for i in input().split()])

ans = 0

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


print(ans%1000000007)

Submission status: Accepted

Solution code:
import sys
import numpy as np

MOD = 10 ** 9 + 7

N, *A = map(int, sys.stdin.read().split())
A = np.array(A, dtype=np.int64)

def main():
    answer = 0
    for n in range(60):
        B = (A >> n) & 1
        x = np.count_nonzero(B)
        y = N - x
        x *= y
        x *= pow(2, n, MOD)
        answer += x
    answer %= MOD
    return answer

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x0 = [0] * 60
x1 = [0] * 60
for i in map(int,input().split()):
  i = bin(i)[2:]
  i = '0'*(60-len(i)) + i
  for j in range(60):
    if i[-j-1] == '0':
      x0[j] += 1
    else:
      x1[j] += 1
ans = 0
for i in range(60):
  ans += (x0[i]*x1[i]*pow(2,i,10**9+7)) % (10**9+7)
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_max = max(a)
l = len(bin(a_max)) - 2
bits = [[0, 0] for _ in range(l)]
for num in a:
    for i, bit in enumerate(format(num, 'b').zfill(l)):
        if bit == '0':
            bits[i][0] += 1
        else:
            bits[i][1] += 1

sumation = 0
div = (10**9+7)
for i in range(l):
    sumation += (bits[i][0]) * (bits[i][1]) * (2**(l-i-1) % div)
print(int(sumation % (10**9+7)))

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
binA=[None]*n
for i in range(n):
  binA[i]=bin(A[i])[2:]
  
keta=[0]*60
for i in range(n):
  tar=binA[i]
  for j in range(len(tar)-1,-1,-1):
    if tar[j]=="1":
      keta[len(tar)-1-j]+=1

pair=[0]*60
for i in range(len(keta)):
  pair[i]=keta[i]*(len(A)-keta[i])
  
ans=0
mul=1
DIV=10**9+7
for i in range(len(pair)):
  ans+=pair[i]*mul
  ans%=DIV
  mul*=2
print(ans)

Submission status: Accepted

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

mod = 10**9 + 7

ans = 0
for i in range(61):
    zero = 0
    one = 0
    for ai in a:
        if ai>>i & 1:
            one += 1
        else:
            zero += 1
    ans += zero * one * 2**i
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect, string
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = inf ;count = 0 ;pro = 1

# An efficient Python3 program to compute 
# sum of bitwise OR of all pair 

# Returns sum of bitwise OR 
# of all pairs 
def sumXOR(arr,n):
    ans=0
    for i in range(61):
        zero=0
        one=0
        for ai in arr:
            if ai>>i & 1:
                one+=1
            else:
                zero+=1
        ans+=zero*one*2**i
        ans%=mod
    return ans
        


# driver function 
n = int(input()) 
arr = list(map(int,input().split()))
ans = sumXOR(arr, n); 
print (ans) 

# This code is contributed by saloni1297

Submission status: Accepted

Solution code:

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

MOD = 10**9+7
tmp = max(A)
ans = 0
base = 1

while True:
    num1 = 0
    num0 = 0
    for i,a in enumerate(A):
        if (a%2 == 1):
            num1 += 1
        A[i] = a//2
    num0 = N-num1
    ans += num0 * num1 * base
    ans %= MOD
    
    base *= 2
    tmp //= 2
    if (tmp==0):break

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
binary = [0]*60
for inp in input().split():
    inp = bin(int(inp))[2:][::-1]
    for j, _inp in enumerate(inp):
        if _inp == '1':
            binary[j] += 1
    
ret = 0
pow2 = 1
for elem in binary:
    if elem == 0 or elem == N:
        pass
    else: 
        mn = elem * (N - elem) % (10**9 + 7)
        ret += pow2 * mn 
        ret = ret % (10**9 + 7)
    pow2 = pow2 * 2 % (10**9 + 7)
print(ret)

Submission status: Accepted

Solution code:
import numpy as np

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

ans = 0
for k in range(60):
  x = int(np.sum(A >> k & 1))
  ans += x * (N - x) * 2**k

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

Submission status: Accepted

Solution code:
import numpy as np

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split()))).reshape(n, 1)
mod = 10**9 + 7
bit = np.arange(64).reshape(1, 64)
b = np.sum((a >> bit) & 1, axis=0)
b *= n - b
b %= mod
b *= (1 << np.arange(64)) % mod
b %= mod
ans = 0
for x in b:
    ans += x
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
def bi(x):
    x = int(x)
    i = 0
    o = [0]*61
    while x > 0:
        o[i] = x % 2
        x //= 2
        i += 1
    return o

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

# print(*A,sep='\n')
ruijoo = 1
bbb = 0
B = [0]*N
h = 0
while h < 61:
    i = 0
    isOne = 0
    while i < N:
        aaaa = A[i][h]
        isOne += aaaa
        B[i] = aaaa
        i += 1
    isZero = N - isOne
    bbb += ruijoo * isZero * isOne
    bbb %= 1000000007
    ruijoo *= 2
    ruijoo %= 1000000007
    h += 1

bbb %= 1000000007
print(bbb)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
M = max(a)
p = len(format(M, 'b'))
l = [0 for _ in range(p)]
ans = 0
for i in range(n):
    a_s = format(a[i], '0' + str(p) + 'b')
    for c in range(p):
        if a_s[c] == '1':
            l[c] += 1
for i in range(p):
    c1 = l[i]
    ans += (2**(p - i - 1)) * c1 * (n - c1)
    ans %= (10**9 + 7)
print(ans)

Submission status: Accepted

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


def main():
    n = int(input())
    a = [int(_) for _ in input().split()]
    M = max(a)
    p = len(format(M, 'b'))
    l = [0 for _ in range(p)]
    ans = 0
    for i in range(n):
        a_s = format(a[i], '0' + str(p) + 'b')
        for c in range(p):
            if a_s[c] == '1':
                l[c] += 1
    for i in range(p):
        c1 = l[i]
        ans += (2**(p - i - 1)) * c1 * (n - c1)
        ans %= (10**9 + 7)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

s = [0]*61
for a in range(n):
    for i in range(61):
        if(A[a]>>i&1)==1:
            s[i+1] += 1
ans = 0
for i in range(60):
    # print("a",2**(i-1)*s.get(i,0)*(n-s.get(i,0)))
    # print("b",s.get(i,0))
    # print("c",(n-s.get(i,0)))
    ans += 2**(i)*s[i+1]*(n-s[i+1])
ans = ans % (10**9+7)
print(int(ans))
# print(s)

Submission status: Accepted

Solution code:
words = lambda t : list(map(t, input().split()))
n = int(input())
xs = words(int)

ys = [0]*60
for x in xs:
    for i in range(60):
        if x == 0:
            continue
        ys[i] += x & 1
        x = x >> 1

ans = 0

for i,yi in enumerate(ys):
    ans += 2**(i) * yi * (n-yi)
    ans %= 10**9 + 7
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split())) 
k =  len(bin(max(A))[2:])
A = [bin(i)[2:].zfill(k) for i in A]
mod = 10**9+7
ans = 0
for i in range(k):
  cnt = sum([1 for j in A if j[i]=='1'])
  ans += cnt*(N-cnt)*(2**(k-i-1))
  ans %= mod 
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
 
lis = [0]*60
 
for a in input().split():
    a = int(a)
    for i in range(60):
        lis[i] += a % 2
        a = a//2
ans = 0
for i, j in enumerate(lis):
    ans += j * (N-j) *(2**i)
    ans = ans % (10**9 + 7)
 
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split())) 
k =  len(bin(max(A))[2:])
bit = [bin(i)[2:].zfill(k) for i in A]
mod = 10**9+7
ans = 0
for i in range(k):
  cnt = sum([1 for j in bit if j[i]=='1'])
  ans += cnt*(N-cnt)*(2**(k-i-1))
  ans %= mod  

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split())) 
k =  len(bin(max(A))[2:])
bit = [bin(i)[2:].zfill(k) for i in A]
mod = 10**9+7
c = [0]*k
ans = 0
for i in range(k):
  cnt = sum([1 for j in bit if j[i]=='1'])
  ans += cnt*(N-cnt)*(2**(k-i-1))
  ans %= mod  

print(ans)

Submission status: Accepted

Solution code:
def d_xor_sum_4(MOD=10**9 + 7):
    N = int(input())
    A = [int(i) for i in input().split()]

    ans = 0
    for d in range((len(bin(max(A))) - 2) + 1):
        nonzero = len([1 for a in A if (a >> d) & 1])
        ans += (2**d) * (nonzero) * (N - nonzero)
        ans %= MOD
    return ans

print(d_xor_sum_4())

Submission status: Accepted

Solution code:
MOD = 10**9+7
N = int(input())
A = [int(i) for i in input().split()]
max_length = len(bin(max(A)))-2
ans = 0
for n in range(max_length):
    c0 = 0
    c1 = 0
    for i, a in enumerate(A):
        if a & 1:
            c1 += 1
        else:
            c0 += 1
        A[i] >>= 1
    ans += c0*c1*(2**n)
    ans %= MOD
print(ans)

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):
  Po=sum([1 for j in A if j>>i & 1])
  Ans += Po*(N-Po)*(2**i)
  Ans = Ans % mod
print(Ans)