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()