[BaekJoon] 1016 제곱 ㄴㄴ 수 (Java)

오태호·2023년 5월 25일
0

백준 알고리즘

목록 보기
233/395
post-thumbnail

1.  문제 링크

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

2.  문제


3.  소스코드

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

public class Main {
    static long min, max;

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

        min = scanner.nextLong();
        max = scanner.nextLong();
    }

    static void solution() {
        int len = (int)(max - min + 1);
        int maxSqrt = (int)Math.sqrt(max);

        boolean[] isNotAnswer = new boolean[len];

        for(long num = 2; num <= maxSqrt; num++) {
            long square = num * num;
            long start = min % square == 0 ? min / square : (min / square) + 1;

            for(long multiply = start; multiply * square <= max; multiply++)
                isNotAnswer[(int)(multiply * square - min)] = true;
        }

        int answerNum = 0;
        for(boolean answer : isNotAnswer)
            if(!answer) answerNum++;

        System.out.println(answerNum);
    }

    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();
        }

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

0개의 댓글