목차

돌무더기의 정상화

ps
링크acmicpc.net/…
출처BOJ
문제 번호32382
문제명돌무더기의 정상화
레벨플래티넘 5
분류

배낭 문제

시간복잡도O(n^3*a)
인풋사이즈n<=100, a<=20
사용한 언어Python 3.11
제출기록34068 / 140ms
최고기록140ms
해결날짜2024/10/16

풀이

코드

"""Solution code for "BOJ 32382. 돌무더기의 정상화".

- Problem link: https://www.acmicpc.net/problem/32382
- Solution link: http://www.teferi.net/ps/problems/boj/32382

Tags: [knapsack]
"""

import collections

MOD = 1_000_000_007


def main():
    N = int(input())
    A = [int(x) for x in input().split()]

    if (sum_a := sum(A)) % 2 != 0:
        print('0')
        return

    target = sum_a // 2
    count_by_len = [collections.defaultdict(int) for _ in range(target + 1)]
    count_by_len[0][0] = 1
    for a_i in A:
        for w in reversed(range(a_i, target + 1)):
            for l, count in count_by_len[w - a_i].items():
                count_by_len[w][l + 1] += count

    fact = [v := 1] + [(v := v * i % MOD) for i in range(1, N + 1)]
    answer = (
        sum(
            fact[l] * fact[N - l] * count
            for l, count in count_by_len[target].items()
        )
        % MOD
    )
    print(answer)


if __name__ == '__main__':
    main()