열렬히.뛰기

11051 - 이항계수2

알고리즘: 실전 > 백준 단계별로 풀기: 10번 ~ 25번 > 11051 - 이항계수2

라이브러리 사용

python
""" 이항계수 2 """
# 1번째 방법 : 라이브러리 사용
import sys
input = sys.stdin.readline

from math import factorial
n, k = list(map(int, input().split()))
result = factorial(n) // (factorial(k) * factorial(n-k))
print(result % 10007)

반복으로 풀기

python
""" 이항계수 2 """
# 2번째 방법 : 반복으로 풀기

N, K = map(int, input().split())

val = 0
def factorial(n):
    ans = n
    while True:
        if n <= 2:
            return ans
            break
        ans *= (n-1)
        n -= 1

if N == K or K == 0:
    val = 1
else:
    val = factorial(N)//(factorial(K)*factorial(N-K))

print(val % 10007)

동적 계획법으로 풀기

python
""" 이항계수 2 """
# 3번째 방법 : 동적 계획법
n, k = map(int, input().split())
dp = [[0]*1 for i in range(1001)]
dp[1].append(1)
for i in range(2, 1001):
    for j in range(1, i+1):
        if j == 1:
            dp[i].append(1)
        elif j == i:
            dp[i].append(1)
        else:
            dp[i].append(dp[i-1][j-1] + dp[i-1][j])
print(dp[n+1][k+1] % 10007)