๊ธฐ๋ก๋ฐฉ

BOJ_1644 : ์†Œ์ˆ˜์˜ ์—ฐ์†ํ•ฉ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1644 : ์†Œ์ˆ˜์˜ ์—ฐ์†ํ•ฉ

Soom_1n 2024. 4. 9. 21:34

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

1644๋ฒˆ: ์†Œ์ˆ˜์˜ ์—ฐ์†ํ•ฉ

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 4,000,000)

www.acmicpc.net


 


๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ •์ˆ˜ N์„ ์—ฐ์†๋œ ์†Œ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์—ฐ์†๋œ ์ˆ˜์˜ ํ•ฉ์—์„œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋”ฐ์ง€๋ฏ€๋กœ ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด ๋ฐฐ์—ด์„ ์ด๋™ํ•˜๋ฉฐ N์ด ๋งŒ๋“ค์–ด์ง€๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.*;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) throws IOException {
        // Input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int N = Integer.parseInt(br.readLine());

        if (N == 1) {
            bw.write('0');
            bw.flush();
            return;
        }

        // ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ : ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด
        boolean[] primes = new boolean[N + 1];
        ArrayList<Integer> primesList = new ArrayList<>();

        for (int i = 2; i <= N; i++) {
            if (!primes[i]) {
                int mul = 2;
                while (i * mul <= N) {
                    primes[i * mul++] = true;
                }
                primesList.add(i);
            }
        }

        // ๊ฒฝ์šฐ์˜ ์ˆ˜ ๊ตฌํ•˜๊ธฐ : ํˆฌํฌ์ธํ„ฐ
        int left = 0;
        int right = 0;
        long sum = 2;
        int answer = 0;

        while (left <= right) {
            if (sum < N) { // ํ•ฉ์ด ์ž‘์œผ๋ฉด ์˜ค๋ฅธ์ชฝ +1
                if (++right >= primesList.size()) break;
                sum += primesList.get(right);
            } else if (sum > N) { // ํ•ฉ์ด ํฌ๋ฉด ์™ผ์ชฝ -1
                sum -= primesList.get(left++);
            } else { // ๊ฐ™์œผ๋ฉด ์นด์šดํŠธ 1๋Š˜๋ฆฌ๊ณ  ์™ผ์ชฝ -1
                answer++;
                sum -= primesList.get(left++);
            }
        }

        // Output
        bw.write(Integer.toString(answer));
        bw.flush();
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๋จผ์ € ์†Œ์ˆ˜ ๋ชฉ๋ก์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋ฅผ ์ด์šฉํ•œ๋‹ค.
    • ์†Œ์ˆ˜๋ฅผ ์ฐพ์œผ๋ฉด ArrayList์ธ primeList์— ์ €์žฅํ•œ๋‹ค.
  • primeList์— ์ €์žฅ ๋œ ์†Œ์ˆ˜ ๋ชฉ๋ก์œผ๋กœ ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด ์—ฐ์†๋œ ํ•ฉ์ด N์ด ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.
    • ํ•ฉ์ด N๋ณด๋‹ค ์ž‘์œผ๋ฉด right +1
    • ํ•ฉ์ด N๋ณด๋‹ค ํฌ๋ฉด left + 1
    • ํ•ฉ์ด N๊ณผ ๊ฐ™์œผ๋ฉด answer + 1 ํ›„, left + 1
  • answer์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  •  N์ด 1์ธ ๊ฒฝ์šฐ์— primeList์˜ ํฌ๊ธฐ๊ฐ€ 0์ด๋ฏ€๋กœ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๋‹ค. 1์ธ ๊ฒฝ์šฐ๋Š” ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•œ๋‹ค.
  • ํˆฌ ํฌ์ธํ„ฐ์™€ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋– ์˜ฌ๋ฆฐ ํ›„์—๋Š” ์‰ฝ๊ฒŒ ํ’€์ด ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_3190 : ๋ฑ€  (0) 2024.04.11
BOJ_2143 : ๋‘ ๋ฐฐ์—ด์˜ ํ•ฉ  (0) 2024.04.10
BOJ_12015 : ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด 2  (0) 2024.04.09
BOJ_1005 : ACM Craft  (0) 2024.04.04
BOJ_12100 : 2048 (Easy)  (0) 2024.03.31