BOJ/백준-11051-python

cosmos·2021년 5월 31일
2
post-thumbnail

문제📖

풀이🙏

  • 첫째 줄에 N과 K가 주어진다.
  • 자연수 N과 정수 K가 주어졌을 때 이항 계수를 10,007로 나눈 나머지를 구하는 프로그램을 작성하시오.

코드💻

# boj, 11051 : 이항 계수2, python3
# 정수론 및 조합론
# 다이나믹 프로그래밍
import sys

def choose(cache, times, left):
    if times == 0:
        return left == 0

    if cache[times][left] != -1:
        return cache[times][left]

    cache[times][left] = choose(cache, times-1, left) + choose(cache, times-1, left-1)

    return cache[times][left]

    
def bino_coef(n, k):
    if k > n:
        return 0
    
    cache = [[-1 for _ in range(n+1)] for _ in range(n+1)]

    return choose(cache, n, n-k)

N, K = map(int, sys.stdin.readline().split())

print(bino_coef(N, K) % 10007) 

결과😎

출처 && 깃허브📝

https://www.acmicpc.net/problem/11051
github

0개의 댓글