목차

짝수 행 세기

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호68647
문제명짝수 행 세기
레벨Level 4
분류

동적 계획법

시간복잡도O(c*r^2)
인풋사이즈c<=300, r<=300
사용한 언어Python
해결날짜2021/01/21

풀이

코드

"""Solution code for "Programmers 68647. 짝수 행 세기".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/68647
- Solution link: http://www.teferi.net/ps/problems/programmers/68647
"""

from teflib import combinatorics

MOD = 10**7 + 19


def solution(a):
    row_count = len(a)
    col_count = len(a[0])
    comb_table = combinatorics.CombTable(row_count + 1, MOD)
    dp_cur = [0] * row_count + [1]
    for c in range(col_count):
        dp_cur, dp_prev = [0] * (row_count + 1), dp_cur
        one_count = sum(row[c] for row in a)
        for x in range(one_count + 1):
            y = one_count - x
            for even_row_count in range(x, row_count - y + 1):
                odd_row_count = row_count - even_row_count
                new_even_row_count = even_row_count - x + y
                dp_cur[new_even_row_count] += (
                    dp_prev[even_row_count] *
                    comb_table.get(even_row_count, x) *
                    comb_table.get(odd_row_count, y))
        for i in range(row_count + 1):
            dp_cur[i] %= MOD

    return dp_cur[row_count]