- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 59 lines of Python from the credited upstream file abc173_c.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4def f(c, h_patterns, w_patterns, black_count):5 6 used = [[False for _ in range(len(w_patterns))]7 for _ in range(len(h_patterns))]8 9 for index, h_pattern in enumerate(h_patterns):10 if h_pattern != 1:11 continue12 13 for w_dash in range(len(w_patterns)):14 if not used[index][w_dash] and c[index][w_dash] == '#':15 used[index][w_dash] = True16 black_count -= 117 18 for index, w_pattern in enumerate(w_patterns):19 if w_pattern != 1:20 continue21 22 for h_dash in range(len(h_patterns)):23 if not used[h_dash][index] and c[h_dash][index] == '#':24 used[h_dash][index] = True25 black_count -= 126 27 return black_count28 29 30def main():31 from itertools import product32 33 h, w, k = map(int, input().split())34 c = [list(input()) for _ in range(h)]35 black_count = 036 ans = 037 38 39 40 41 42 for hi in range(h):43 for wi in range(w):44 if c[hi][wi] == '#':45 black_count += 146 47 for h_patterns in product([1, 0], repeat=h):48 for w_patterns in product([1, 0], repeat=w):49 fi = f(c, h_patterns, w_patterns, black_count)50 51 if fi == k:52 ans += 153 54 print(ans)55 56 57if __name__ == '__main__':58 main()59