[백준 백트래킹, 중복조합] N과 M (4)(python)

이진규·2022년 2월 8일
1

백준(PYTHON)

목록 보기
39/115

문제

https://www.acmicpc.net/problem/15652

나의 코드

"""
1. 아이디어


2. 시간복잡도

"""

# ① 재귀를 이용한 백트래킹 풀이
from sys import stdin
input = stdin.readline

n, m = map(int, input().split())
res = []

def btr(v, dep):
    if dep == m:
        print(*res)
        return

    for i in range(v, n+1):
        res.append(i)
        btr(i, dep+1)
        res.pop()

btr(1, 0)

# ② 중복조합 라이브러리를 이용한 풀이
from itertools import combinations_with_replacement
from sys import stdin
input = stdin.readline

n, m = map(int, input().split())
nums = [ x for x in range(1, n+1) ]

c = list(combinations_with_replacement(nums, m)) # - 중복조합

for num in c:
    print(*num)
    
profile
항상 궁금해하고 공부하고 기록하자.

0개의 댓글