Rock and Lever (Codeforces 1420B)
Tags: Math, bitmasks

  "You must lift the dam. With a lever. I will give it to you.You must block the canal. With a rock. I will not give the rock to you." Danik urgently needs rock and lever! Obviously, the easiest way to get these things is to ask Hermit Lizard for them.Hermit Lizard agreed to give Danik the lever. But to get a stone, Danik needs to solve the following task.You are given a positive integer $$$n$$$, and an array $$$a$$$ of positive integers. The task is to calculate the number of such pairs $$$(i,j)$$$ that $$$i<j$$$ and $$$a_i$$$ $$$\&$$$ $$$a_j \ge a_i \oplus a_j$$$, where $$$\&$$$ denotes the bitwise AND operation, and $$$\oplus$$$ denotes the bitwise XOR operation.Danik has solved this task. But can you solve it?

Input: Each test contains multiple test cases.The first line contains one positive integer $$$t$$$ ($$$1 \le t \le 10$$$) denoting the number of test cases. Description of the test cases follows.The first line of each test case contains one positive integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — length of the array.The second line contains $$$n$$$ positive integers $$$a_i$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output: For every test case print one non-negative integer — the answer to the problem.

Note: In the first test case there is only one pair: $$$(4,7)$$$: for it $$$4$$$ $$$\&$$$ $$$7 = 4$$$, and $$$4 \oplus 7 = 3$$$.In the second test case all pairs are good.In the third test case there are two pairs: $$$(6,5)$$$ and $$$(2,3)$$$.In the fourth test case there are no good pairs.

Sample Inputs:
5
5
1 4 3 7 10
3
1 1 1
4
6 2 5 3
2
2 4
1
1

Sample Outputs:
1
3
2
0
0

Solution (Python):
from functools import *
from itertools import *
from collections import *
from math import *
from bisect import *

def factors(n):
    i = 1
    res = []
    while i <= sqrt(n):
        if n % i == 0:
            if n / i == i:
                res.append(i)
            else:
                res.append(i)
                res.append(n // i)
        i = i + 1
    return res

def prime_factors(n):
    i = 2
    factors = []
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
            factors.append(i)
    if n > 1:
        factors.append(n)
    return factors

def largest_prime_factor(n):
    i = 2
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
    return n
for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    res = 0
    data = defaultdict(int)
    for i in arr:
        res += data[i.bit_length()]
        data[i.bit_length()] += 1
    print(res)

Solution (Python):
from math import log2
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    b = [0] * 32
    for i in range(n):
        idx = int(log2(a[i]))
        b[idx] += 1
    res = 0
    for i in range(32):
        res += b[i] * (b[i] - 1) // 2
    print(res)

Solution (Python):
for _ in range(int(input())):
    __ = input()
    freq = {}
    ans = 0
    for num in list(map(int, input().split())):
        freq[num.bit_length()] = freq.get(num.bit_length(), 0) + 1
    for (l, f) in freq.items():
        ans += f * (f - 1) // 2
    print(ans)