728x90

< 처음 풀 때 >

 

중복 순열인 것은 알고 있었다. 그래서 초기의 리스트에 *b 만큼 해주어서 리스트의 길이를 늘린 후, set 을 통해 중복을 제거하고자 했지만 시간 초과가 났다. product 라는 함수를 알게 되었다.

 

< 해결 방법 > 

product 를 이용해서 해결 하였다. 하지만 계속 int 오류가 발생 하였고 알아보니 product 함수는 repeat= 라는 표현을 뒤에 붙여 주어야 사용이 가능했다. 

 

import sys
import math
from itertools import product
from itertools import combinations_with_replacement
 
a,b = map(int, sys.stdin.readline().split(" "))
 
k = [ ]
 
for i in range(1,a+1):
    k.append(i)
 
q = list(product(k,repeat=b))
 
for i in q:
    print(*i)
728x90

+ Recent posts