Post

[BOJ] 1010. ๋‹ค๋ฆฌ ๋†“๊ธฐ(JAVA)

๐Ÿ“Œ๋ฌธ์ œ

๋ฌธ์ œ

์žฌ์›์ด๋Š” ํ•œ ๋„์‹œ์˜ ์‹œ์žฅ์ด ๋˜์—ˆ๋‹ค. ์ด ๋„์‹œ์—๋Š” ๋„์‹œ๋ฅผ ๋™์ชฝ๊ณผ ์„œ์ชฝ์œผ๋กœ ๋‚˜๋ˆ„๋Š” ํฐ ์ผ์ง์„  ๋ชจ์–‘์˜ ๊ฐ•์ด ํ๋ฅด๊ณ  ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์žฌ์›์ด๋Š” ๋‹ค๋ฆฌ๊ฐ€ ์—†์–ด์„œ ์‹œ๋ฏผ๋“ค์ด ๊ฐ•์„ ๊ฑด๋„ˆ๋Š”๋ฐ ํฐ ๋ถˆํŽธ์„ ๊ฒช๊ณ  ์žˆ์Œ์„ ์•Œ๊ณ  ๋‹ค๋ฆฌ๋ฅผ ์ง“๊ธฐ๋กœ ๊ฒฐ์‹ฌํ•˜์˜€๋‹ค. ๊ฐ• ์ฃผ๋ณ€์—์„œ ๋‹ค๋ฆฌ๋ฅผ ์ง“๊ธฐ์— ์ ํ•ฉํ•œ ๊ณณ์„ ์‚ฌ์ดํŠธ๋ผ๊ณ  ํ•œ๋‹ค. ์žฌ์›์ด๋Š” ๊ฐ• ์ฃผ๋ณ€์„ ๋ฉด๋ฐ€ํžˆ ์กฐ์‚ฌํ•ด ๋ณธ ๊ฒฐ๊ณผ ๊ฐ•์˜ ์„œ์ชฝ์—๋Š” N๊ฐœ์˜ ์‚ฌ์ดํŠธ๊ฐ€ ์žˆ๊ณ  ๋™์ชฝ์—๋Š” M๊ฐœ์˜ ์‚ฌ์ดํŠธ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜๋‹ค. (N โ‰ค M)

์žฌ์›์ด๋Š” ์„œ์ชฝ์˜ ์‚ฌ์ดํŠธ์™€ ๋™์ชฝ์˜ ์‚ฌ์ดํŠธ๋ฅผ ๋‹ค๋ฆฌ๋กœ ์—ฐ๊ฒฐํ•˜๋ ค๊ณ  ํ•œ๋‹ค. (์ด๋•Œ ํ•œ ์‚ฌ์ดํŠธ์—๋Š” ์ตœ๋Œ€ ํ•œ ๊ฐœ์˜ ๋‹ค๋ฆฌ๋งŒ ์—ฐ๊ฒฐ๋  ์ˆ˜ ์žˆ๋‹ค.) ์žฌ์›์ด๋Š” ๋‹ค๋ฆฌ๋ฅผ ์ตœ๋Œ€ํ•œ ๋งŽ์ด ์ง€์œผ๋ ค๊ณ  ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ์ชฝ์˜ ์‚ฌ์ดํŠธ ๊ฐœ์ˆ˜๋งŒํผ (N๊ฐœ) ๋‹ค๋ฆฌ๋ฅผ ์ง€์œผ๋ ค๊ณ  ํ•œ๋‹ค. ๋‹ค๋ฆฌ๋ผ๋ฆฌ๋Š” ์„œ๋กœ ๊ฒน์ณ์งˆ ์ˆ˜ ์—†๋‹ค๊ณ  ํ•  ๋•Œ ๋‹ค๋ฆฌ๋ฅผ ์ง€์„ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ.

์ž…๋ ฅ

์ž…๋ ฅ์˜ ์ฒซ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ ๊ฐ๊ฐ์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์— ๋Œ€ํ•ด ๊ฐ•์˜ ์„œ์ชฝ๊ณผ ๋™์ชฝ์— ์žˆ๋Š” ์‚ฌ์ดํŠธ์˜ ๊ฐœ์ˆ˜ ์ •์ˆ˜ N, M (0 < N โ‰ค M < 30)์ด ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ ์กฐ๊ฑดํ•˜์— ๋‹ค๋ฆฌ๋ฅผ ์ง€์„ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


๐Ÿ’ช์•„์ด๋””์–ด

  1. m๊ฐœ ์ค‘ n๊ฐœ ๋ฝ‘๊ธฐ(์กฐํ•ฉ)
    • m๊ฐœ์˜ ์ˆซ์ž ์ค‘ n๊ฐœ๋ฅผ ๋ฝ‘์•„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์กฐํ•ฉ
    • ์„œ๋กœ ๊ฒน์น˜๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณตX, arr[i] < arr[i+1]์ด์–ด์•ผ ํ•œ๋‹ค.
    • ์กฐํ•ฉ์€ ์ˆœ์„œ์™€ ์ƒ๊ด€์—†๊ธฐ ๋•Œ๋ฌธ์— arr[i] < arr[i+1]์„ ๊ณ ๋ คํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
  2. nCr = n-1Cr + n-1Cr-1 ๊ณต์‹ ์‚ฌ์šฉ
    • N์ด 30์ด๊ธฐ ๋•Œ๋ฌธ์— ์žฌ๊ท€๋กœ ํ’€๋ฉด N!์œผ๋กœ ์‹œ๊ฐ„์ดˆ๊ณผ
  3. dp๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๋ณต๋˜๋Š” ๊ณ„์‚ฐ ์ค„์ด๊ธฐ
    • mem[n-1][r-1] != -1์ด๋ฉด ์ด๋ฏธ ๊ณ„์‚ฐ๋œ ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๊ฐ’์„ ๋ฐ˜ํ™˜
    • mem[n-1][r-1] = combi(n-1, r-1) + combi(n-1, r)๋กœ ๊ณ„์‚ฐํ•˜๊ณ  mem[n-1][r-1]์— ์ €์žฅ

๐Ÿฅ‚์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.*;
import java.util.*;

public class Main {

  static int[][] mem;
  static ArrayDeque<Integer> arr = new ArrayDeque<>();
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int T = Integer.parseInt(br.readLine());
    StringBuilder sb = new StringBuilder();
    StringTokenizer st;
    int N, M;
    for (int i = 0; i < T; i++) {
      st = new StringTokenizer(br.readLine(), " ");
      N = Integer.parseInt(st.nextToken());
      M = Integer.parseInt(st.nextToken());
      mem = new int[M][N];
      for (int j = 0; j < M; j++) {
        Arrays.fill(mem[j], -1);
      }
      sb.append(combi(M,N)).append("\n");
    }
    System.out.println(sb);
  } // end of class
  public static int combi(int n, int r) {
    if(r == 0 || n == r) {
      return 1;
    }
    if (mem[n - 1][r - 1] != -1) {
      return mem[n - 1][r - 1];
    }
    return mem[n - 1][r - 1] = combi(n - 1, r - 1) + combi(n - 1, r);
  }
}// end of main
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.