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: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(bin(_)[2:]) for _ in list(map(int, input().split()))]
ans = 0
mod = 10 ** 9 + 7
for i in range(N - 1):
    for j in range(i + 1, N):
        Xor = list(str(A[i] + A[j]))
        for k in range(len(Xor)):
            if Xor[k] == "1":
                ans += 2**(len(Xor) - k - 1)

print(ans % mod)

Submission status: Time Limit Exceeded

Solution code:
#xorの性質
#AxorA=0、交換法則、結合法則が成立、bitごとに独立して考える！
n=int(input())
a=list(map(int,input().split()))
res=0
for j in range(60):
  s=0 #1の数
  for i in a:
    if (i>>j & 0b1)==0b1:
      s+=1
  r=2**j*(n-s)*s
  res=(res+r)%(10**9+7)
print(res)

Submission status: Time Limit Exceeded

Solution code:
#xorの性質
#AxorA=0、交換法則、結合法則が成立、bitごとに独立して考える！
n=int(input())
a=list(map(int,input().split()))
res=0
for j in range(60):
  s=0 #1の数
  for i in a:
    if (i>>j & 0b1)==0b1:
      s+=1
  res+=2**j*(n-s)*s
print(res%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(s) for s in input().split()]
d = 0
ans = 0
mod = 10**9+7
for d in range(60):
	c0 = 0
	c1 = 0
	for v in a:
		if v >> d & 1:
			c1 += 1
		else:
			c0 += 1
	ans = (ans + (c0 * c1 << d)) % mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
M=10**9+7
n=int(input())
l=list(map(int,input().split()))
a=0
for i in range(60):
  t=sum(j>>i&1 for j in l)
  a+=(t*(n-t)<<i)%M
print(a%M)

Submission status: Time Limit Exceeded

Solution code:
M=10**9+7
n=int(input())
l=list(map(int,input().split()))
a=0
for i in range(60):
  t=sum((l[j]>>i)%2 for j in range(n))
  a+=(t*(n-t)<<i)%M
print(a%M)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


def cal_xor(max_num, cnt, exp):
    return cnt * (max_num - cnt) * (2 ** exp)


dig = len(format(max(a), 'b'))
bi_digit_cnt = [0] * dig
for i in a:
    bi_i = format(i, 'b').zfill(dig)
    for j in range(len(bi_i)):
        if bi_i[-1-j] == '1':
            bi_digit_cnt[j] += 1

ans = 0

for i in range(dig):
    ans += cal_xor(n, bi_digit_cnt[i], i)
    ans %= MOD

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
p = 10**9 + 7
num_a = [0]*60
str_a = []
for i in a:
  a_str = format(i, 'b').zfill(60)
  str_a.append(a_str)
  for j in range(len(a_str)):
    if a_str[-1-j] == '1':
      num_a[j] += 1
ans = 0
cnt = 0

for i, j in enumerate(num_a):
  ans =  (ans + j*(n-j)*2**i)%p

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
p = 10**9 + 7
num_a = [0]*60
str_a = []
for i in a:
  a_str = format(i, 'b').zfill(60)
  str_a.append(a_str)
  for j in range(len(a_str)):
    if a_str[-1-j] == '1':
      num_a[j] += 1
ans = 0
cnt = 0
for i in str_a:
  cnt += 1
  if cnt >= n:
    break
  for j in range(len(i)):
    if i[-1-j] == '1':
      num_a[j] -= 1
      ans += (n-cnt-num_a[j])*2**j
      ans = ans % p
    else:
      ans += num_a[j]*2**j % p
      ans = ans % p
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    A = list(map(int, input().split()))
    MAX_BIT = 60
    MOD = 10 ** 9 + 7

    data = [0] * (MAX_BIT + 2)
    for a in A:
        for bit in range(MAX_BIT + 1):
            if a & (1 << bit):
                data[bit] += 1
    ans = 0
    for i, d in enumerate(data):
        if d == 0:
            continue
        ans += pow(2, i, MOD) * d * (N-d)
        ans %= MOD
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    N = int(input())
    *a, = map(int, input().split())

    b = [0] * 60
    for x in a:
        for j in range(60):
            b[j] += (x & 1) > 0
            x >>= 1

    t = 1
    ans = 0
    for j in range(60):
        ans = (ans + b[j] * (N - b[j]) * t) % MOD
        t <<= 1
    print(ans)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def main():    
    MOD = 10 ** 9 + 7
    n = int(readline())
    a = list(map(int, readline().split()))
    ans = 0
    for i in range(60):
        x = 0
        for j in range(n):
            if a[j] >> i & 1:
                x += 1
        ans += x * (n - x) * 2 ** i
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())
p = 10**9+7
A = [list(format(a, '060b')) for a in A]
s = 0
for i in range(60):
    Ai = [a[i] for a in A]
    c0 = Ai.count('0')
    c1 = n-c0
    c = c0*c1
    s = (s+c*(2**(59-i))) % p
print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())
p = 10**9+7
A = [list(format(a, '060b')) for a in A]
s = 0
for i in range(60):
    Ai = [int(a[i]) for a in A]
    c0 = Ai.count(0)
    c1 = Ai.count(1)
    c = c0*c1
    s = (s+c*(2**(59-i))) % p
print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
*A, = map(int, input().split())
p = 10**9+7
A = [list(format(a, '060b'))[::-1] for a in A]
A = [[int(a[i]) for a in A] for i in range(60)]
s = 0
for i in range(60):
    Ai = A[i]
    c0 = Ai.count(0)
    c1 = Ai.count(1)
    c = c0*c1
    s = (s+c*(2**i)) % p
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def LIST(): return list(map(int, input().split()))
mod = 10 ** 9 + 7

n = INT()
a = LIST()
arr = [0 for _ in range(61)]
for i in range(1, n + 1):
    bn = bin(a[i - 1])[2:]
    l = len(bn)
    for j in range(l):
        arr[j + 61 - l] += int(bn[j])
ans = 0
for i in range(61):
    ans *= 2
    arri = arr[i]
    ans += arri * (n - arri if arri != 0 else 0)
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def LIST(): return list(map(int, input().split()))
mod = 10 ** 9 + 7

#0埋め　桁合わせ
def digit(n, r):
    n = str(n)
    l = len(n)
    n = '0' * (r - l) + n
    return n

n = INT()
a = LIST()
arr = [0 for _ in range(61)]
arr2 = [0 for _ in range(61)]
for i in range(1, n + 1):
    bn = bin(a[i - 1])[2:]
    for j in range(len(bn)):
        arr[j + 61 - len(bn)] += int(bn[j])
for i in range(61):
    arr2[i] = n - arr[i]
    if arr2[i] == n:
        arr2[i] = 0
ans = 0
for i in range(61):
    ans *= 2
    ans += arr[i] * arr2[i]
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def LIST(): return list(map(int, input().split()))
mod = 10 ** 9 + 7

#0埋め　桁合わせ
def digit(n, r):
    n = str(n)
    l = len(n)
    n = '0' * (r - l) + n
    return n

n = INT()
a = LIST()
arr = [0 for _ in range(61)]
arr2 = [0 for _ in range(61)]
for i in range(1, n + 1):
    bn = digit(bin(a[i - 1])[2:], 61)
    for j in range(61):
        arr[j] += int(bn[j])
for i in range(61):
    arr2[i] = n - arr[i]
    if arr2[i] == n:
        arr2[i] = 0
ans = 0
for i in range(61):
    ans *= 2
    ans += arr[i] * arr2[i]
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def LIST(): return list(map(int, input().split()))
mod = 10 ** 9 + 7

#0埋め　桁合わせ
def digit(n, r):
    n = str(n)
    l = len(n)
    for i in range(r - l):
        n = '0' + n
    return n

n = INT()
a = LIST()
arr = [0 for _ in range(61)]
arr2 = [0 for _ in range(61)]
for i in range(1, n + 1):
    bn = digit(bin(a[i - 1])[2:], 61)
    for j in range(61):
        arr[j] += int(bn[j])
for i in range(61):
    arr2[i] = n - arr[i]
    if arr2[i] == n:
        arr2[i] = 0
ans = 0
for i in range(61):
    ans *= 2
    ans += arr[i] * arr2[i]
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = True

#0埋め　桁合わせ
def digit(n, r):
    n = str(n)
    l = len(n)
    for i in range(r - l):
        n = '0' + n
    return n

n = INT()
a = LIST()
arr = [0 for _ in range(61)]
arr2 = [0 for _ in range(61)]
for i in range(1, n + 1):
    bn = digit(bin(a[i - 1])[2:], 61)
    for j in range(61):
        arr[j] += int(bn[j])
        arr2[j] += int(not int(bn[j]))
for i in range(61):
    if arr2[i] == n:
        arr2[i] = 0
ans = 0
for i in range(61):
    ans *= 2
    ans += arr[i] * arr2[i]
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

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

n = inp()
a = inpl()
lmx = 0
for i in range(n):
    tmp = bin(a[i])
    a[i] = tmp[2:]
    a[i] = a[i][::-1]
    lmx = max(lmx, len(tmp)-2)
res = 0
cnt = [[0,0] for _ in range(lmx)]
for i in range(n):
    bi = a[i]
    ln = len(bi)
    for j in range(lmx):
        if j < ln:
            cnt[j][int(bi[j])] += 1
        else:
            cnt[j][0] += 1
for i,bit in enumerate(cnt):
    x,y = bit
    res += x*y*pow(2,i)
    res %= mod
print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import *
from heapq import *
import sys

sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]
dij = [(1, 0), (-1, 0), (0, 1), (0, -1)]

class mint:
    def __init__(self, x):
        self.__x = x % md

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

    def __neg__(self):
        return mint(-self.__x)

    def __add__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x + other)

    def __sub__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x - other)

    def __rsub__(self, other):
        return mint(other - self.__x)

    def __mul__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x * other)

    __radd__ = __add__
    __rmul__ = __mul__

    def __truediv__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x * pow(other, md - 2, md))

    def __rtruediv__(self, other):
        return mint(other * pow(self.__x, md - 2, md))

    def __pow__(self, power, modulo=None):
        return mint(pow(self.__x, power, md))

md = 10**9+7

def main():
    ans=mint(0)
    n=II()
    aa=LI()
    for i in range(61):
        cnt1=0
        for a in aa:cnt1+=a>>i&1
        ans+=(1<<i)*cnt1*(n-cnt1)
    print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_li = list(map(int,input().split()))
ans = 0
for x in range(n-1):
    for y in range(x+1,n):
        ans += a_li[x] ^ a_li[y]
print(ans % (10**9+7))

Submission status: Time Limit Exceeded

Solution code:
MOD = 10 ** 9 + 7

def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    max_bin_digits = len(bin(max(A))[2:])
    cnts = [0] * max_bin_digits

    for a in A:
        bin_a = bin(a)[2:].zfill(max_bin_digits)
        for i, b in enumerate(bin_a):
            if int(b):
                cnts[-i-1] += 1

    ans = 0
    for i in range(max_bin_digits):
        ans += cnts[i] * (N - cnts[i]) * 2 ** i
        ans %= MOD

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
MOD = 10 ** 9 + 7

def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    max_bin_digits = len(bin(max(A))[2:])
    cnts = [[0, 0] for _ in range(max_bin_digits)]

    for a in A:
        bin_a = bin(a)[2:].zfill(max_bin_digits)
        for i, b in enumerate(bin_a):
            cnts[-i-1][int(b)] += 1

    ans = 0
    for i in range(max_bin_digits):
        ans += cnts[i][0] * cnts[i][1] * 2 ** i
        ans %= MOD

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
MOD = 10 ** 9 + 7

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

    max_bin_digits = len(bin(max(A))[2:])
    cnts = [[0, 0] for _ in range(max_bin_digits)]

    for a in A:
        bin_a = bin(a)[2:].zfill(max_bin_digits)
        bin_a = bin_a[::-1]
        for i, b in enumerate(bin_a):
            cnts[i][int(b)] += 1

    ans = 0
    for i in range(max_bin_digits):
        ans += cnts[i][0] * cnts[i][1] * 2 ** i
        ans %= MOD

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

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

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    n = II()
    A = LI()
    # print([bin(a) for a in A])
    mod = pow(10, 9) + 7
    ans = 0
    for i in range(0, 61):
        # i桁目が1の個数を数える
        x = 0
        for a in A:
            if (a >> i) & 1:
                x += 1
        y = n - x       # i桁目が0の個数
        # print(i, x, y)
        # XORなので x*y 通りの組み合わせ
        ans = ans + pow(2, i) * x * y % mod
    print(ans % mod)



if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
  a[i]=bin(a[i])
  a[i]=a[i][2:]
  a[i]=a[i].zfill(60)
  a[i]=list(map(int,a[i]))
totl=0
p=10**9+7
for k in range(60):
  zero=0
  for i in range(n):
    if a[i][k]==0:
      zero+=1
  num=zero*(n-zero)
  if num==0:
    continue
  for j in range(59-k):
    num=num*2
    if num>=p:
      num=num%p
  totl=(totl+num)
  if totl>=p:
    totl=totl%p
print(totl)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
  a[i]=bin(a[i])
  a[i]=a[i][2:]
  a[i]=a[i].zfill(60)
  a[i]=list(map(int,a[i]))
totl=0
p=10**9+7
for k in range(60):
  zero=0
  for i in range(n):
    if a[i][k]==0:
      zero+=1
  totl+=zero*(n-zero)*(2**(59-k))
print(totl%p)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
  a[i]=list(bin(a[i]))
  a[i]=a[i][2:]
  a[i].reverse()
  l=len(a[i])
  if l!=61:
    for j in range(61-l):
      a[i].append('0')
  a[i].reverse()
  a[i]=list(map(int,a[i]))
totl=0
p=10**9+7
for k in range(61):
  zero=0
  for i in range(n):
    if a[i][k]==0:
      zero+=1
  num=zero*(n-zero)
  for j in range(60-k):
    num=num*2
    if num>=p:
      num=num%p
  totl+=num
print(totl%p)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
  a[i]=list(bin(a[i]))
  a[i]=a[i][2:]
  a[i].reverse()
  l=len(a[i])
  if l!=61:
    for j in range(61-l):
      a[i].append('0')
  a[i].reverse()
  a[i]=list(map(int,a[i]))
totl=0
p=10**9+7
for k in range(61):
  zero=0
  for i in range(n):
    if a[i][k]==0:
      zero+=1
  totl+=zero*(n-zero)*(2**(60-k))
print(totl%p)

Submission status: Time Limit Exceeded

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

M = max(A)
m = M
a = 1
while m // 2 != 0:
  m = m//2
  a += 1
#aはMの桁数

b = 0
for i in range(a):
  c = 0
  for j in range(N):
    c += (A[j] & 2**i)//(2**i)  #A[j]の2**iの位の値をcに加えている
  b = (b + c*(N-c)*pow(2,i,10**9+7)) % (10**9+7)
  
print(b)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(x) for x in input().split()]
MOD = 1000000007
ans = 0
for d in range(60):
    ones = 0
    for a in l:
        if a >> d & 1:
            ones += 1
    zeroes = n - ones
    ans += pow(2, d, MOD) * ones % MOD * zeroes % MOD
ans %= MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
As = list(map(int, input().split()))

def to_bit(n):
    res = np.zeros(60, dtype=int)
    for digit in range(59, -1, -1):
        res[digit] = n % 2
        n >>= 1
    return res

P = 1e9 + 7
nums = np.array([to_bit(a) for a in As])
num1 = nums.sum(axis=0)
num0 = N - num1

res = 0
for prod in num0 * num1:
    res <<= 1
    res += prod
    res = int(res % P)
print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
mod = 10**9+7
result = [0 for _ in range(120)]
for i in a:
    x = format(i, "060b")[::-1]
    for j in range(60):
        result[j] = result[j]+(x[j]=="1")
result = [(n-i)*i for i in result]
for i in range(119):
    result[i+1] += result[i]//2
    result[i] = result[i]%2    
print(int("".join(map(str, result[::-1])), 2)%mod)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
n = int(input())
a = np.array(sys.stdin.read().split(), dtype=np.int64)
mod = 10 ** 9 + 7
ans = [0] * 60
for x in a:
    x = bin(x)[2:].zfill(60)
    for i in range(60):
        ans[i] += int(x[i])
for i in range(60):
    ans[i] = ans[i] * (n - ans[i]) % mod
    ans[i] = ans[i] * pow(2, 59 - i, mod) % mod
ans_sum = 0
for x in ans:
    ans_sum = (ans_sum + x) % mod
print(ans_sum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())), dtype=np.int64)
mod = 10 ** 9 + 7
ans = [0] * 60
for x in a:
    x = bin(x)[2:].zfill(60)
    for i in range(60):
        ans[i] += int(x[i])
for i in range(60):
    ans[i] = ans[i] * (n - ans[i]) % mod
    ans[i] = ans[i] * pow(2, 59 - i, mod) % mod
ans_sum = 0
for x in ans:
    ans_sum = (ans_sum + x) % mod
print(ans_sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
ans = [0] * 60
for x in a:
    x = bin(x)[2:].zfill(60)
    for i in range(60):
        ans[i] += int(x[i])
for i in range(60):
    ans[i] = ans[i] * (n - ans[i]) % mod
    ans[i] = ans[i] * pow(2, 59 - i, mod) % mod
ans_sum = 0
for x in ans:
    ans_sum = (ans_sum + x) % mod
print(ans_sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
mod = int(1e9+7)
A.sort(reverse=True)
for i in range(N):
    A[i] = format(A[i], 'b')
    A[i] = A[i].zfill(len(A[0]))
ans = 0
for i in range(len(A[0])):
    count_1 = 0
    count_0 = 0
    for a in A:
        if a[i] == '0':
            count_0 += 1
        else:
            count_1 += 1
    ans += count_0 * count_1 * pow(2, len(A[0])-i-1, mod) 
    ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
mod=1000000007
arr=list(map(int,input().split()))
su=0
for i in range(n):
    for j in range(i+1,n):
        su+=(arr[i]^arr[j])%mod
print(su%mod)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
arr=list(map(int,input().split()))
su=0
for i in range(n):
    for j in range(i+1,n):
        su+=arr[i]^arr[j]
print(su%1000000007)

Submission status: Time Limit Exceeded

Solution code:
MOD = 10**9 + 7
class ModInt:
    def __init__(self, x):
        if isinstance(x, str):
            x = int(x)
        self.x = x % MOD

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

    __repr__ = __str__

    def __add__(self, other):
        return (
            ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other)
        )

    def __sub__(self, other):
        return (
            ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other)
        )

    def __mul__(self, other):
        return (
            ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other)
        )

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

    def __pow__(self, other):
        return (
            ModInt(pow(self.x, other.x, MOD)) if isinstance(other, ModInt) else
            ModInt(pow(self.x, other, MOD))
        )

    __radd__ = __add__

    def __rsub__(self, other):
        return (
            ModInt(other.x - self.x) if isinstance(other, ModInt) else
            ModInt(other - self.x)
        )

    __rmul__ = __mul__

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

    def __rpow__(self, other):
        return (
            ModInt(pow(other.x, self.x, MOD)) if isinstance(other, ModInt) else
            ModInt(pow(other, self.x, MOD))
        )

def encode(N, A, max_k):
    bits = [0 for _ in range(max_k)]
    for i in range(N):
        a = bin(A[i])[2:]
        for k in range(len(a) - 1, -1, -1):
            bits[len(a) - 1 - k] += int(a[k])
    return bits

def decode(N, bits):
    ret = ModInt(0)
    for k, counter in enumerate(bits):
        ret += 2 ** k * (counter * (N - counter))
    return ret

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

print(decode(N, encode(N, A, max_k)))

Submission status: Time Limit Exceeded

Solution code:
MOD = 10**9 + 7
class ModInt:
    def __init__(self, x):
        if isinstance(x, str):
            x = int(x)
        self.x = x % MOD

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

    __repr__ = __str__

    def __add__(self, other):
        return (
            ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other)
        )

    def __sub__(self, other):
        return (
            ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other)
        )

    def __mul__(self, other):
        return (
            ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other)
        )

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

    def __pow__(self, other):
        return (
            ModInt(pow(self.x, other.x, MOD)) if isinstance(other, ModInt) else
            ModInt(pow(self.x, other, MOD))
        )

    __radd__ = __add__

    def __rsub__(self, other):
        return (
            ModInt(other.x - self.x) if isinstance(other, ModInt) else
            ModInt(other - self.x)
        )

    __rmul__ = __mul__

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

    def __rpow__(self, other):
        return (
            ModInt(pow(other.x, self.x, MOD)) if isinstance(other, ModInt) else
            ModInt(pow(other, self.x, MOD))
        )

def encode(N, A, max_k):
    bits = [[0, 0] for _ in range(max_k)]
    for i in range(N):
        a = format(A[i], 'b').zfill(max_k)
        for k in range(max_k):
            if int(a[k]) == 0:
                bits[max_k - 1 - k][0] += 1
            else:
                bits[max_k - 1 - k][1] += 1
    return bits

def decode(bits):
    ret = ModInt(0)
    for k, counter in enumerate(bits):
        ret += 2 ** k * (counter[0] * counter[1])
    return ret

N = int(input())
A = list(map(int, input().split()))
max_k = len(bin(max(A))) - 2

print(decode(encode(N, A, max_k)))

Submission status: Time Limit Exceeded

Solution code:
MOD = 10**9 + 7
class ModInt:
    def __init__(self, x):
        if isinstance(x, str):
            x = int(x)
        self.x = x % MOD

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

    __repr__ = __str__

    def __add__(self, other):
        return (
            ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other)
        )

    def __sub__(self, other):
        return (
            ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other)
        )

    def __mul__(self, other):
        return (
            ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other)
        )

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

    def __pow__(self, other):
        return (
            ModInt(pow(self.x, other.x, MOD)) if isinstance(other, ModInt) else
            ModInt(pow(self.x, other, MOD))
        )

    __radd__ = __add__

    def __rsub__(self, other):
        return (
            ModInt(other.x - self.x) if isinstance(other, ModInt) else
            ModInt(other - self.x)
        )

    __rmul__ = __mul__

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

    def __rpow__(self, other):
        return (
            ModInt(pow(other.x, self.x, MOD)) if isinstance(other, ModInt) else
            ModInt(pow(other, self.x, MOD))
        )

def encode(N, A, max_k):
    bits = [[0, 0] for _ in range(max_k)]
    for i in range(N):
        a = format(A[i], 'b').zfill(max_k)
        for k in range(max_k):
            if int(a[k]) == 0:
                bits[max_k - 1 - k][0] += 1
            else:
                bits[max_k - 1 - k][1] += 1
    return bits

def decode(bits):
    return sum([ModInt(2 ** k * (counter[0] * counter[1])) for k, counter in enumerate(bits)])

N = int(input())
A = list(map(int, input().split()))
max_k = len(bin(max(A))) - 2

print(decode(encode(N, A, max_k)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
MOD = 1000000007
ans = 0
for digit in range(60):
    ones = 0
    for num in a:
        if num >> digit & 1:
            ones += 1
    zeroes = n - ones  # Zeros?
    ans = (ans + ones * zeroes * pow(2, digit, MOD)) % MOD

print(ans)