본문 바로가기
Project/Algorithm

[백준 알고리즘] 1010 다리 놓기

by 도낙원 2017. 11. 14.
반응형

 다이나믹 프로그래밍 분류에 있는 다리 놓기 문제입니다.

이 문제는 두가지 방법으로 풀 수 있습니다.

조합과 다이나믹 프로그래밍 방법 두 가지로 풀 수 있는데

오늘은 조합으로 풀이를 하고 나중에 다이나믹으로 풀어보도록하겠습니다.


  문제



  해석


#1 조합

     조합으로도 문제를 풀 수 있습니다. 하지만 조합으로 문제를 풀면 int형 범위를 넘어서기 때문에 BigInteger를 사용하였습니다.


  코드


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package DinamicAlgo;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.StringTokenizer;
 
public class Baek1010 {
 
    public void solve() {
        int T = sc.nextInt();
 
        BigInteger result;
        while (T-- > 0) {
            int n = sc.nextInt();
            int m = sc.nextInt();
            System.out.println(combination(n, m));
        }
 
    }
 
    public static BigInteger combination(int n, int m) {
        BigInteger result = new BigInteger("1");
        BigInteger divide = new BigInteger("1");
        if (m == n)
            return result;
 
        for (int i = 1; i <= n; i++) {
            result = result.multiply(BigInteger.valueOf(m));
            result = result.divide(BigInteger.valueOf(i));
            m--;
        }
 
        return result;
    }
 
    public static void main(String[] args) {
        sc.init();
 
        new Baek1010().solve();
    }
 
    static class sc {
 
        private static StringTokenizer st;
        private static BufferedReader br;
 
        static void init() {
            br = new BufferedReader(new InputStreamReader(System.in));
            st = new StringTokenizer("");
        }
 
        static String readLine() {
            try {
                return br.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
 
        static String readLineReplace() {
            try {
                return br.readLine().replaceAll("\\s+""");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
 
        static String next() {
            while (!st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
 
        static int nextInt() {
            return Integer.parseInt(next());
        }
 
        static Long nextLong() {
            return Long.parseLong(next());
        }
 
        static double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}
 
cs


반응형
사업자 정보 표시
난길샵 | 박현숙 | 경상북도 성주군 월항면 수죽길 98길 | 사업자 등록번호 : 256-07-01668 | TEL : 010-9909-8420 | Mail : skr04@naver.com | 통신판매신고번호 : 제2020-경북성주-52호 | 사이버몰의 이용약관 바로가기

'Project > Algorithm' 카테고리의 다른 글

[TopCoder] 고장난 로봇  (1) 2017.11.12
[TopCoder] 초급 - 암호  (0) 2017.10.25
[TopCoder] 초급 - 전체 탐색  (0) 2017.10.24
[TopCoder] 초급 - 시뮬레이션  (0) 2017.10.24
[TopCoder] 탑코더를 시작하며  (0) 2017.10.24

댓글