To solve this problem, we need to count the number of pairs (i, j) such that the sum of elements from two arrays (A[i]) and (B[j]) is a power of two.
Approach
- Frequency Map: First, create a frequency map for array (B) to quickly look up how many times each element appears in (B). This helps in efficiently checking if the complement (power of two minus element from (A)) exists in (B).
- Generate Powers of Two: Precompute all possible powers of two up to a sufficiently large value (e.g., (2^{31})) since this covers the maximum possible sum of elements from two arrays (assuming elements are up to (10^9)).
- Count Valid Pairs: For each element in (A), check against all precomputed powers of two. For each power of two, compute the complement (power minus the element from (A)) and use the frequency map to find how many times this complement exists in (B). Add this count to the total number of valid pairs.
Solution Code
from collections import Counter
def count_power_pairs(A, B):
freq_B = Counter(B)
powers = []
current = 1
# Generate powers of two up to 2^31 (covers sum of two 1e9 elements)
for _ in range(32):
powers.append(current)
current *= 2
total = 0
for a in A:
for p in powers:
complement = p - a
if complement in freq_B:
total += freq_B[complement]
return total
# Example usage:
# A = [1, 2, 3]
# B = [3, 4, 5]
# print(count_power_pairs(A, B)) # Output: 2
Explanation
- Frequency Map: Using
Counterfrom thecollectionsmodule allows us to get the count of each element in (B) in (O(n)) time where (n) is the length of (B). - Powers of Two: Generating up to (2^{31}) ensures we cover all possible sums of elements from (A) and (B) (since (2^{31}) is larger than the sum of two elements each up to (10^9)).
- Efficiency: For each element in (A), we check 32 powers of two (a constant number), so the overall time complexity is (O(m \times 32 + n)) where (m) is the length of (A) and (n) is the length of (B). This is efficient for large input sizes.
This approach ensures we quickly and correctly count all valid pairs where the sum is a power of two.
Answer: The function count_power_pairs returns the number of valid pairs. For example, if the input arrays are (A = [1,2,3]) and (B = [3,4,5]), the output is 2. The final answer should be the result of this function for the given input arrays.
\boxed{2} (for example input) or replace with the actual result based on the problem's input. However, since the problem's input wasn't provided, the code above is the solution. If you have specific input values, you can run the code to get the answer. For the purpose of this problem, the code is the solution.
But if we assume the problem expects the code as the answer, then the code is the solution. However, in most programming contest settings, the answer is the numerical result. Since the input wasn't provided, I'll leave the code as the solution.
Alternatively, if the problem's input was, say, (A = [1]) and (B = [1]), the answer is \boxed{1}.
But given the ambiguity of the input (since the image was missing), the code is the correct approach.
from collections import Counter
def count_power_pairs(A, B):
freq_B = Counter(B)
powers = []
current = 1
for _ in range(32):
powers.append(current)
current *= 2
total = 0
for a in A:
for p in powers:
complement = p - a
if complement in freq_B:
total += freq_B[complement]
return total
# Read input (assuming input is given as two lines of space-separated integers)
import sys
input = sys.stdin.read().split()
n = int(input[0])
m = int(input[1])
A = list(map(int, input[2:2+n]))
B = list(map(int, input[2+n:2+n+m]))
print(count_power_pairs(A, B))
This code reads the input, processes it, and prints the result. The final answer is the output of this code for the given input. For example, if input is:
3 3
1 2 3
3 4 5
The output is 2, so the answer is \boxed{2}. \boxed{2}


作者声明:本文包含人工智能生成内容。