본문 바로가기

알고리즘/Python

[Python | SWEA] 4837. 부분집합의 합

https://www.youtube.com/watch?v=zasMkhSD2pc&list=PLodgw23vNd_U66omABrprSwJBZhKPFGMM&index=2 

 

답안

def dfs(index, cnt, total):
    global answer
    A = 12

    # 가지치기
    if total>K:
        return

    # 종료 조건
    if index==A:
        if total==K and cnt==CNT:
            answer+=1
        return

    dfs(index+1, cnt+1, total+index+1)
    dfs(index+1, cnt, total)


T = int(input())
for test_case in range(1, T+1):
    CNT, K = list(map(int, input().split()))
    answer = 0
    dfs(0,0,0)

    print(f"#{test_case} {answer}")