개발/algorithm
[백준 1182번] 부분수열의 합 - python
zzi_on2
2022. 1. 29. 19:50
문제 링크
https://www.acmicpc.net/problem/1182
1182번: 부분수열의 합
첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1 ≤ N ≤ 20, |S| ≤ 1,000,000) 둘째 줄에 N개의 정수가 빈 칸을 사이에 두고 주어진다. 주어지는 정수의 절댓값은 100,000을 넘지 않는다.
www.acmicpc.net
풀이
- 조합을 사용해서 풀었는데 다행히 시간초과가 안났다.
from itertools import combinations
n, s = map(int,input().split())
data = list(map(int,input().split()))
count = 0
# 모든 부분 수열의 합을 구해서 s와 같을 때 count + 1
for i in range(1, n+1):
combine = list(combinations(data,i))
for num in combine:
if sum(num) == s :
count += 1
print(count)