Editorial for Lecture Gap
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
The gap condition makes the sequence strictly spaced. Shift it back to an
ordinary non-decreasing sequence by setting
.
Then becomes
, and the bounds become
.
Let . If
, no such
exists and the answer is
(do not evaluate a
binomial coefficient with a negative upper index).
Otherwise the number of non-decreasing sequences of length with values in
is combinations with repetition:
.
Special cases worth checking by hand:
: no forced gap, so the answer is
(vanilla non-decreasing sequences in
);
: the sequence is strictly increasing, so
and the answer is
;
:
and the answer is
.
Precompute factorials and inverse factorials modulo up to
. Time is
.
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, g = map(int, input().split())
m = n - (k - 1) * g
if m < 1:
print(0)
else:
print(nCr(m + k - 1, k))
Comments