Владимир Овсянников
Ученик
(106)
1 месяц назад
def count_codes(b, n, t, reports):
from itertools import product
masks = []
sums = []
for i in range(t):
mask, s = reports[2 * i], reports[2 * i + 1]
masks.append(mask)
sums.append(int(s))
def is_valid(code):
for mask, expected_sum in zip(masks, sums):
current_sum = sum(int(code[i]) for i in range(n) if mask[i] == '1')
if current_sum != expected_sum:
return False
return True
all_codes = product(range(b), repeat=n)
valid_count = sum(1 for code in all_codes if is_valid(code))
return valid_count
b = int(input().strip())
n = int(input().strip())
t = int(input().strip())
reports = [input().strip() for _ in range(2 * t)]
result = count_codes(b, n, t, reports)
print(result)