[백준] 2014번 소수의 곱 ★★★

거북이·2023년 2월 23일
0

백준[골드1]

목록 보기
1/2
post-thumbnail

💡문제접근

  • 오랜 시간 고민했는데 중복을 제거해주는 방법을 몰라서 결국 구글링을 통해서 해결했다.

💡코드(메모리 : 52756KB, 시간 : 328ms)

import heapq
import sys
input = sys.stdin.readline

K, N = map(int, input().strip().split())
li = list(map(int, input().strip().split()))

heap = []
for i in li:
    heapq.heappush(heap, i)

for i in range(N):
    num = heapq.heappop(heap)
    for j in range(K):
        temp = num * li[j]
        heapq.heappush(heap, temp)
        # 중복을 제거하는 방법(2×5나 5×2는 동일하므로)
        if num % li[j] == 0:
            break
else:
    print(num)

💡소요시간 : 1h 20m

0개의 댓글