[BaekJoon] 2014 소수의 곱 (Java)

오태호·2023년 6월 20일
0

백준 알고리즘

목록 보기
255/395
post-thumbnail

1.  문제 링크

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

2.  문제

3.  소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {
    static int K, N;
    static long[] nums;
    static PriorityQueue<Long> multiplies;

    static void input() {
        Reader scanner = new Reader();

        K = scanner.nextInt();
        N = scanner.nextInt();
        nums = new long[K];
        multiplies = new PriorityQueue<>();

        for(int idx = 0; idx < K; idx++) {
            nums[idx] = scanner.nextLong();
            multiplies.offer(nums[idx]);
        }
    }

    static void solution() {
        long answer = 0;

        while(N-- > 0) {
            answer = multiplies.poll();

            for(int idx = 0; idx < K; idx++) {
                if((answer * nums[idx]) >= ((long) 2 << 30)) break;

                multiplies.offer(answer * nums[idx]);

                if(answer % nums[idx] == 0) break;
            }
        }

        System.out.println(answer);
    }

    public static void main(String[] args) {
        input();
        solution();
    }

    static class Reader {
        BufferedReader br;
        StringTokenizer st;

        public Reader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while(st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }
    }
}
profile
자바, 웹 개발을 열심히 공부하고 있습니다!

0개의 댓글