문제 링크 : https://www.acmicpc.net/problem/2407


대충보고 쉬운 문제인줄 알고 생각없이 코딩하고 테케를 넣어봤는데 결과값이 너무 커서 자료형 범위를 넘어가서 답이 안나왔다.


할수 없이 오랜만에 이클립스를 켰다. 자바 만세.


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
import java.math.BigInteger;
import java.util.Scanner;
 
public class Main {
    
    public static void main(String[] args) {
        int n, m;
        BigInteger bm, bj;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        
        bm = facto(n-m).multiply(facto(m));
        bj = facto(n);
        
        System.out.println(bj.divide(bm));
    }
    
    public static BigInteger facto(int a) {
        if(a==0return BigInteger.valueOf(1);
        
        BigInteger ret = new BigInteger("1");
        for(int i=1; i<=a; i++) {
            ret = ret.multiply(BigInteger.valueOf(i));
        }
        return ret;
    }
}
cs






'Algorithm > 백준' 카테고리의 다른 글

백준 14500번 테트로미노  (0) 2019.04.26
백준 13460번 구슬 탈출 2  (0) 2019.03.08
백준 10974 모든 순열  (0) 2019.02.25
백준 1764 듣보잡  (0) 2019.02.20
백준 1779번 비숍  (0) 2019.01.29

+ Recent posts