[SWEA] 2817. 부분 수열의 합

야금야금 공부·2023년 5월 10일
0

SWEA

목록 보기
31/43
post-thumbnail

2817. 부분 수열의 합


문제 풀이

t = int(input())

for i in range(1, t+1):

    n, k = map(int, input().split())
    arr = list(map(int, input().split()))
    ans = 0

    def powerset(idx, total):
        global ans
        if total == k:  # total이 k와 같으면 횟수 ans += 1
            ans += 1
            return

        if idx >= n:   # n을 넘으면 return
            return

        powerset(idx + 1, total + arr[idx])
        powerset(idx + 1, total)

    powerset(0, 0)
    print(f"#{i} {ans}")

0개의 댓글