다리 놓기

last update: 2021.09.08.WED

1010 다리 놓기, Silver 5

Memory 32748KB, Time 108ms


import operator
from functools import reduce
import sys

T = int(input())
cases = [list(map(int, sys.stdin.readline().split())) for _ in range(T)]
def nCr(n, r):
    if n < 1 or r < 0 or n < r:
        raise ValueError
    r = min(r, n-r)
    numerator = reduce(operator.mul, range(n, n-r, -1), 1)
    denominator = reduce(operator.mul, range(r, 1, -1), 1)

    return numerator // denominator

for i in range(T):
    print(nCr(cases[i][1], cases[i][0]))



  • 런타임 에러가 났던 코드
import operator
from functools import reduce
#from itertools import combinations


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

def nCr(n, r):
    if n < 1 or r < 0 or n < r:
        raise ValueError
    r = min(r, n-r)
    numerator = reduce(operator.mul, range(n, n-r, -1), 1)
    denominator = reduce(operator.mul, range(r, 1), 1)

    return numerator // denominator

print(nCr(M, N))