-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaekjoon_2014.java
132 lines (106 loc) · 3.15 KB
/
baekjoon_2014.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.ssafy.algo;
import java.util.Scanner;
public class baekjoon_2014 {
static final int MAX_K = 541;
static boolean [] chk = new boolean[MAX_K * MAX_K];
private static int K;
private static int N;
private static long[] prime;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// data input
K = sc.nextInt();
N = sc.nextInt();
prime = new long[K];
MyPriorityQueue q = new MyPriorityQueue();
for (int i = 0; i < K; i++) {
prime[i] = sc.nextInt();
q.heapPush(prime[i]);
}
// solve
// 소수 리스트, 소수와 비교하여 곱한 값 넣는 큐, 최종 리스트
// 큐의 맨 앞 요소를 빼서 각 소수를 곱하면서 큐에 다시 넣는다. => 현재 최소값을 기준으로 소수 목록을 곱하여 pq에 삽입
//mygumi.tistory.com/183 [마이구미의 HelloWorld]
long head = 0;
for (int i = 0; i < N; i++) {
// n번째 뺀 값이 n번째 수가 된다.
head = q.heapPop();
// 큐를 활용하여 삽입마다 오름차순으로 정렬됨으로써 원하는 값들을 리스트에 저장 가능.
for (int j = 0; j < K; j++) {
long value = head * prime[j];
q.heapPush(value);
if (head % prime[j] == 0) {
break;
}
}
/* for(int j = 0; j < q.heapSize; ++j)
System.out.print(q.heap[j] + " ");
System.out.println();*/
}
// data output
System.out.println(head);
}
}
class MyPriorityQueue{
final int MAX_SIZE = 1000000;
long heap[] = new long[MAX_SIZE];
int heapSize = 0;
void heapInit()
{
heapSize = 0;
}
void heapPush(long value)
{
if (heapSize + 1 > MAX_SIZE)
{
return;
}
heap[heapSize] = value;
int current = heapSize;
while (current > 0 && heap[current] < heap[(current - 1) / 2])
{
long temp = heap[(current - 1) / 2];
heap[(current - 1) / 2] = heap[current];
heap[current] = temp;
current = (current - 1) / 2;
}
heapSize = heapSize + 1;
}
boolean isEmpty() {
if(heapSize <= 0)
return true;
return false;
}
long heapPop()
{
if (heapSize <= 0)
{
return -1;
}
long value = heap[0];
heapSize = heapSize - 1;
heap[0] = heap[heapSize];
int current = 0;
while (current < heapSize && current * 2 + 1 < heapSize)
{
int child;
if (current * 2 + 2 >= heapSize)
{
child = current * 2 + 1;
}
else
{
child = heap[current * 2 + 1] < heap[current * 2 + 2] ? current * 2 + 1 : current * 2 + 2;
}
if (heap[current] < heap[child])
{
break;
}
long temp = heap[current];
heap[current] = heap[child];
heap[child] = temp;
current = child;
}
return value;
}
}