https://www.acmicpc.net/problem/2407
2407번: 조합
n과 m이 주어진다. (5 ≤ n ≤ 100, 5 ≤ m ≤ 100, m ≤ n)
www.acmicpc.net
[난이도] Silver2
[유형] 수학
[풀이]
nCm 공식을 적용하여 분자, 분모를 각각 계산한 뒤 분자/분모를 해준것이 정답입니다.
n,m이 100 제한이라 unsigned long 범위도 넘어가므로 BigInteger를 이용해야 합니다.
C++같이 BigInteger를 지원하지 않는 경우에는 String을 통한 덧셈 연산으로 해결해야 합니다.
import java.io.BufferedReader
import java.io.InputStreamReader
import java.math.BigInteger
import java.util.*
fun main() = with(BufferedReader(InputStreamReader(System.`in`))){
val (N,M) = readLine().split(' ').map{it.toInt()}
var up = BigInteger("1")
var down = BigInteger("1")
for(i in 1..M){
up = up.multiply(BigInteger((N-i+1).toString()))
down = down.multiply(BigInteger(i.toString()))
}
println(up.divide(down))
}
https://github.com/has2/Problem-Solving/blob/master/boj-solved.ac/Silver2/2407.cpp
'Problem-Solving > BOJ' 카테고리의 다른 글
[BOJ/백준][Gold5] 13172 : Σ (Kotlin) (0) | 2021.08.15 |
---|---|
[BOJ/백준][Silver1] 11660 : 구간 합 구하기5 (Kotlin) (0) | 2021.08.15 |
[BOJ/백준][Silver1] 1991 : 트리 순회 (Kotlin) (0) | 2021.08.15 |
[BOJ/백준][Silver3] 15652 : N과 M (4) (Kotlin) (0) | 2021.08.06 |
[BOJ/백준][Silver3] 15650 : N과 M (2) (Kotlin) (0) | 2021.08.06 |