Editorial for Stickers
Use this editorial only when stuck, and do not 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.
Approach
The stickers are identical and the people are distinct, so a distribution is a tuple of non-negative integers (x1,…,xk) with x1+⋯+xk=n.
This is stars and bars: the number of solutions is
(k−1n+k−1).
When n=0 this is (k−1k−1)=1 (everyone gets nothing). When k=1 this is (0n)=1 (the only person gets every sticker).
Precompute factorials and inverse factorials modulo 109+7 up to n+k−1≤2⋅106. Then (k−1n+k−1)=(n+k−1)!⋅(k−1)!−1⋅n!−1. Time is O(n+k) for the precomputation.
Solution (Python)
MOD = 10**9 + 7
MAXN = 2 * 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
def nCr(n, k):
if k < 0 or k > n:
return 0
return fact[n] * invfact[k] % MOD * invfact[n - k] % MOD
n, k = map(int, input().split())
print(nCr(n + k - 1, k - 1))
Comments0
No comments yet
Be the first to comment.
New comment
Log in to join the discussion.