[BaekJoon] 11689 GCD(n, k) = 1 (Java)

오태호·2023년 6월 19일
0

백준 알고리즘

목록 보기
254/395
post-thumbnail

1.  문제 링크

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

2.  문제


3.  소스코드

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

public class Main {
    static long n;

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

        n = scanner.nextLong();
    }

    static void solution() {
        long pi = n;
        for(long num = 2; num * num <= n; num++) {
            if(n % num == 0) pi = pi / num * (num - 1);
            while(n % num == 0) n /= num;
        }

        if(n != 1) pi = pi / n * (n - 1);
        System.out.println(pi);
    }

    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개의 댓글