코딩테스트

[백준] 15651 | N과 M (3) [파이썬/python]

사용할수없는닉네임이왜이렇게많지ㅠㅠ 2025. 3. 24. 22:59
728x90

문제

link: https://www.acmicpc.net/problem/15651

중복순열을 구하는 문제이다.

 

접근

백트래킹 알고리즘을 이용해 코드를 작성했다. N과 M (2)의 코드를 변형하여 사용했다.

https://going-on.tistory.com/17

 

[백준] 15650 | N과 M (2) [파이썬/python]

문제link: https://www.acmicpc.net/problem/15650입력받은 값에 따라 조합을 구하는 문제이다. 접근이전에 N과 M (1)을 풀 때 itertools를 사용했기 때문에 이번에는 itertools 대신 백트래킹 알고리즘을 활용해

going-on.tistory.com

 

코드

import sys
input = sys.stdin.readline

def permutation_rep():
    if len(tmp) == M:
        print(" ".join(map(str, tmp)))
        return
    
    for i in range(1, N + 1):
        tmp.append(i)
        permutation_rep()
        tmp.pop()

N, M = map(int, input().split())
tmp = []            
permutation_rep()
728x90