# BOJ02407 조합
https://www.acmicpc.net/problem/2407 (opens new window)
nCm을 출력한다.
# 조합
조합 공식은 아래와 같다.
nCr = n! / (n - r)! * r!
# int와 long의 한계
int
:-2,147,483,648
~2,147,483,647
long
:-9,223,372,036,854,775,808
~9,223,372,036,854,775,807
# BigInteger
문자열 형태로 숫자를 다룬다. 즉 숫자의 범위가 무한에 가깝다.
# 제출 코드
아래는 최종 제출 코드이다.
public class BOJ2407 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
BigInteger number1 = BigInteger.ONE;
BigInteger number2 = BigInteger.ONE;
for (int i = 0; i < m; i++) {
number1 = number1.multiply(new BigInteger(String.valueOf(n - i)));
number2 = number2.multiply(new BigInteger(String.valueOf(i + 1)));
}
System.out.println(number1.divide(number2));
}
}
# References
[Java] 큰 숫자(정수) 다루기 BigInteger 사용법 & 예제 총정리 (opens new window)