๊ธฐ๋ก๋ฐฉ

BOJ_1456 : ๊ฑฐ์˜ ์†Œ์ˆ˜ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1456 : ๊ฑฐ์˜ ์†Œ์ˆ˜

Soom_1n 2024. 2. 18. 01:13

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

 

1456๋ฒˆ: ๊ฑฐ์˜ ์†Œ์ˆ˜

์–ด๋–ค ์ˆ˜๊ฐ€ ์†Œ์ˆ˜์˜ N์ œ๊ณฑ(N ≥ 2) ๊ผด์ผ ๋•Œ, ๊ทธ ์ˆ˜๋ฅผ ๊ฑฐ์˜ ์†Œ์ˆ˜๋ผ๊ณ  ํ•œ๋‹ค. ๋‘ ์ •์ˆ˜ A์™€ B๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, A๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , B๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฑฐ์˜ ์†Œ์ˆ˜๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net



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

  • ์–ด๋–ค ์†Œ์ˆ˜์˜ N์ œ๊ณฑ (N>2)์˜ ์ˆ˜๋ฅผ ๊ฑฐ์˜ ์†Œ์ˆ˜๋ผ๊ณ  ํ•œ๋‹ค.
  • A์ด์ƒ B์ดํ•˜์˜ ๊ฑฐ์˜ ์†Œ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. (1<=A<=B<=10^14)

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

  • 10^14 ๊นŒ์ง€์˜ ์ˆ˜์—์„œ ๊ฑฐ์˜ ์†Œ์ˆ˜๋ฅผ ์ฐพ์œผ๋ ค๋ฉด, ๊ทธ ์ œ๊ณฑ๊ทผ์ธ 10^7๊นŒ์ง€์˜ ์†Œ์ˆ˜๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค.
    • 10^7์˜ ์ œ๊ณฑ์ด 10^14์ด๋ฏ€๋กœ, ์ ์–ด๋„ 10^7๊นŒ์ง€๋Š” ์†Œ์ˆ˜๋ฅผ ์ฐพ์•„์•ผ N์ œ๊ณฑ ๊ณ„์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • 10^7๊นŒ์ง€์˜ ์†Œ์ˆ˜๋Š” ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋กœ ๊ตฌํ•œ๋‹ค.
  • ๊ตฌํ•œ ๊ฐ๊ฐ์˜ ์†Œ์ˆ˜์—์„œ N์ œ๊ณฑํ•œ ๊ฐ’์ด B๋ณด๋‹ค ์ปค์งˆ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. ๊ทธ ์ค‘ A๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ์นด์šดํŠธํ•œ๋‹ค.
    • N์ œ๊ณฑ์˜ ๊ฐ’์ด longํ˜•์„ ์ดˆ๊ณผํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ดํ•ญ ์ •๋ฆฌ๋กœ ํ•ด๊ฒฐํ•œ๋‹ค
    • N^k <= B  ====> N <= B / N^(k-1)

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

import java.io.*;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        final int MAX = 10_000_001;

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());

        long A = Long.parseLong(st.nextToken());
        long B = Long.parseLong(st.nextToken());
        boolean[] pn = new boolean[MAX];

        for (int i = 2; i <= Math.sqrt(MAX); i++) {
            if (!pn[i]) {
                int mul = 2;
                while (i * mul < MAX) {
                    pn[i * mul++] = true;
                }
            }
        }

        int cnt = 0;
        for (int i = 2; i < MAX; i++) {
            if (!pn[i]) {
                long n = i;
                while ((double) i <= (double) B / (double) n) {
                    if ((double) i >= (double) A / (double) n) {
                        cnt++;
                    }
                    n *= i;
                }
            }
        }

        bw.write(Integer.toString(cnt));
        bw.flush();
    }
}

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

  • ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋กœ ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด boolean ๋ฐฐ์—ด pn์„ ๋งŒ๋“ค์–ด 10^7์˜ ์ œ๊ณฑ๊ทผ๊นŒ์ง€ ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.
  • ๊ฐ ์†Œ์ˆ˜์˜ N์ œ๊ณฑ ๊ฒฐ๊ณผ๊ฐ€ A~B์ธ ๊ฒฝ์šฐ๋ฅผ ์นด์šดํŠธํ•œ๋‹ค.
    • N์ œ๊ณฑ ๊ฒฐ๊ณผ์—์„œ longํ˜•์„ ๋„˜์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ดํ•ญ ์ •๋ฆฌ๋ฅผ ํ†ตํ•ด ๋น„๊ตํ•œ๋‹ค.
  • ์นด์šดํŠธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋Š” ์ต์ˆ™ํ•œ๋ฐ, ์ดํ•ญ ์ •๋ฆฌ์— ๋Œ€ํ•œ ์•„์ด๋””์–ด๊ฐ€ ์ƒ์†Œํ–ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค.
    • longํ˜•์„ ๋„˜๋Š”๋‹ค๋Š” ๊ณ„์‚ฐ์ด ์˜ˆ์ƒํ•˜๊ธฐ ํž˜๋“ค์—ˆ๋‹ค.

 

728x90