A kitchen has a menu of dishes, where deliciousness[i] is the tastiness score of the i-th dish. A good meal is formed by picking two different dishes, at indices i < j, such that deliciousness[i] + deliciousness[j] is a power of two, meaning the sum equals 2k for some integer k≥0 (that is, 1, 2, 4, 8, ...). Note that 0 is not a power of two.
Dishes at different indices are treated as different items, even when their scores are equal. Each unordered pair of indices is counted once.
Return the number of good meals that can be formed. Since the count can be very large, return it modulo 1000000007.
Function Signature
def count_good_meals(deliciousness: list[int]) -> int:
Rules
-
A good meal is a pair of indices
i < j
. Two dishes with the same score at different indices form a valid pair, and no dish pairs with itself.
-
The powers of two are 1, 2, 4, 8 and so on. A sum of 0 never counts.
-
Return the count modulo
1000000007
.
Constraints
-
1 <= len(deliciousness) <= 10^5
-
0 <= deliciousness[i] <= 2^20
(that is,
1048576
)
Examples
Input: deliciousness = [2, 2, 6, 1, 3, 5]
Output: 5
The good meals are (2, 2) with sum 4, (2, 6) twice with sum 8 (once for each 2), (1, 3) with sum 4, and (3, 5) with sum 8, for 5 in total.
Input: deliciousness = [0, 0, 1, 1, 3]
Output: 7
Each 0 pairs with each 1 for a sum of 1 (4 pairs), the two 1s sum to 2 (1 pair), and each 1 pairs with the 3 for a sum of 4 (2 pairs). The two 0s sum to 0, which does not count, so the total is 7.
Input: deliciousness = [1048576, 1048576]
Output: 1
The two largest allowed scores add up to 2097152, which is 221, so this single pair is a good meal.