巨蟒(56)
f=lambda n,k:k<1and 1or f(n-1,k-1)*n/k;print f(*input())
非高尔夫代码和一些用于计算二项式系数的捷径的解释。(注意:我只是想出一些洞见才能了解39个字符的版本;我认为这种方法不会帮助您。)
# Since choose(n,k) =
#
# n!/((n-k)!k!)
#
# [n(n-1)...(n-k+1)][(n-k)...(1)]
# = -------------------------------
# [(n-k)...(1)][k(k-1)...(1)]
#
# We can cancel the terms:
#
# [(n-k)...(1)]
#
# as they appear both on top and bottom, leaving:
#
# n (n-1) (n-k+1)
# - ----- ... -------
# k (k-1) (1)
#
# which we might write as:
#
# choose(n,k) = 1, if k = 0
# = (n/k)*choose(n-1, k-1), otherwise
#
def choose(n,k):
if k < 1:
return 1
else:
return choose(n-1, k-1) * n/k
# input() evaluates the string it reads from stdin, so "5,2" becomes
# (5,2) with no further action required on our part.
#
# In the golfed version, we make use of the `*` unpacking operator,
# to unpack the tuple returned by input() directly into the arguments
# of f(), without the need for intermediate variables n, k at all.
#
n, k = input()
# This line is left as an exercise to the reader.
print choose(n, k)