본문 바로가기
알고리즘/백준

백준 2004번 - 조합 0의 개수 (Java 8)

by latissimus 2022. 3. 15.

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

 

2004번: 조합 0의 개수

첫째 줄에 정수 $n$, $m$ ($0 \le m \le n \le 2,000,000,000$, $n \ne 0$)이 들어온다.

www.acmicpc.net

코드 :

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

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());

        int five = fivePowCount(N) - fivePowCount(N - M) - fivePowCount(M);
        int two = twoPowCount(N) - twoPowCount(N - M) - twoPowCount(M);
        System.out.println(Math.min(five,two));
    }

    public static int fivePowCount(int num) {
        int fiveCount = 0;
            while (num >= 5) {
                fiveCount += num/ 5;
                num /= 5;
            }
        return fiveCount;
    }
    public static int twoPowCount(int num) {
        int twoCount = 0;
        while (num >= 2) {
            twoCount += num/ 2;
            num /= 2;
        }
        return twoCount;
    }
}

참고 :

https://st-lab.tistory.com/166

댓글