Editorial for Presenters


Approach

A presenting sequence is an ordered selection of kk distinct people out of nn. That is the falling factorial (a permutation)

P(n,k)=n!/(n−k)!P(n, k) = n! / (n - k)!

when 0≤k≤n0 \le k \le n. If k>nk > n there are not enough people, so the answer is 00.

This is not a combination (nk)\binom{n}{k}: 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 (k=0k = 0) is 11, including the case n=k=0n = k = 0.

Precompute factorials and inverse factorials modulo 109+710^9 + 7 up to 10610^6, then answer in constant time as n!⋅(n−k)!−1n! \cdot (n - k)!^{-1}. Time is O(106)O(10^6) for the precomputation.

Solution (Python)

Code 1
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)

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.