문제 링크
풀이
import math
t = int(input())
for i in range(t):
n, m = map(int, input().split())
print(math.factorial(m) // (math.factorial(m-n) * math.factorial(n)))
다리를 짓는 경우의 수는 다리가 놓일 위치 M개 중에 다리의 개수인 N개를 고르는 경우의 수이므로, mCn과 같다.
mCn = m!/(m-n)!*n! 이므로, math 모듈의 factorial을 이용하여 풀었다.
math 모듈을 활용하지 않고 factorial을 직접 구현하여 풀면 다음과 같다.
def factorial(x):
if x <= 1:
return 1
else:
return factorial(x-1) * x
t = int(input())
for i in range(t):
n, m = map(int, input().split())
print(factorial(m) // (factorial(m-n) * factorial(n)))
'연습장 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 9012] 괄호 with Python (0) | 2021.03.30 |
---|---|
[백준 10828] 스택 with Python (0) | 2021.03.26 |
[백준 10870] 피보나치 수 5 with Node.js (0) | 2021.03.26 |
[백준 2798] 블랙잭 with Node.js (0) | 2021.03.25 |
[백준 11721] 열 개씩 끊어 출력하기 with Node.js (0) | 2021.03.25 |