Powers of Two
You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2 (i. e. some integer x exists so that ai + aj = 2x).
In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).In the second example all pairs of indexes (i, j) (where i < j) include in answer.
The first line contains the single positive integer n (1 ≤ n ≤ 105) — the number of integers.The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Print the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2.
from collections import Counter
n = int(input())
a = Counter([int(x) for x in input().split()])
r = 0
for (k, v) in a.items():
    u = (1 << (k - 1).bit_length()) - k
    if u:
        if u in a:
            r += v * a[u]
    else:
        r += v * (v - 1) // 2
print(r)
import math

def nextpower2(n):
    return math.pow(2, int(math.log(n, 2)) + 1)
n = int(input())
l = list(map(int, input().split()))
count = 0
l.sort()
s = {}
for i in range(len(l)):
    if nextpower2(l[i]) - l[i] in s:
        count += s[nextpower2(l[i]) - l[i]]
    if l[i] in s:
        s[l[i]] += 1
    else:
        s[l[i]] = 1
print(count)
import collections
input()
C = collections.Counter(map(int, input().split()))
s = 0
for (x, w) in C.items():
    y = (1 << x.bit_length()) - x
    s += w * C[y] if x > y else w * (w - 1) >> 1
print(s)