Editorial for Presenters
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Approach
A presenting sequence is an ordered selection of distinct people out of
. That is the
falling factorial (a permutation)
when . If
there are not enough people, so the answer is
.
This is not a combination : swapping two chosen people produces a different
speaking order, so two sequences that use the same set of people still count separately.
The empty product () is
, including the case
.
Precompute factorials and inverse factorials modulo up to
, then answer in
constant time as
. Time is
for the precomputation.
Solution (Python)
MOD = 10**9 + 7
MAXN = 10**6 + 5
fact = [1] * MAXN
for i in range(1, MAXN):
fact[i] = fact[i - 1] * i % MOD
invfact = [1] * MAXN
invfact[MAXN - 1] = pow(fact[MAXN - 1], MOD - 2, MOD)
for i in range(MAXN - 2, -1, -1):
invfact[i] = invfact[i + 1] * (i + 1) % MOD
n, k = map(int, input().split())
if k > n:
print(0)
else:
print(fact[n] * invfact[n - k] % MOD)
Comments