Editorial for Lecture Gap


Approach

The gap condition xi+1≥xi+gx_{i+1} \ge x_i + g makes the sequence strictly spaced. Shift it back to an ordinary non-decreasing sequence by setting

yi=xi−(i−1)gy_i = x_i - (i - 1)g.

Then xi+1≥xi+gx_{i+1} \ge x_i + g becomes yi+1≥yiy_{i+1} \ge y_i, and the bounds become

1≤y1≤y2≤⋯≤yk≤n−(k−1)g1 \le y_1 \le y_2 \le \cdots \le y_k \le n - (k - 1)g.

Let m=n−(k−1)gm = n - (k - 1)g. If m<1m < 1, no such yy exists and the answer is 00 (do not evaluate a binomial coefficient with a negative upper index).

Otherwise the number of non-decreasing sequences of length kk with values in {1,…,m}\{1, \ldots, m\} is combinations with repetition:

(m+k−1k)\binom{m + k - 1}{k}.

Special cases worth checking by hand:

  • g=0g = 0: no forced gap, so the answer is (n+k−1k)\binom{n + k - 1}{k} (vanilla non-decreasing sequences in [1,n][1, n]);
  • g=1g = 1: the sequence is strictly increasing, so m=n−k+1m = n - k + 1 and the answer is (nk)\binom{n}{k};
  • k=1k = 1: m=nm = n and the answer is nn.

Precompute factorials and inverse factorials modulo 109+710^9 + 7 up to n+kn + k. Time is O(n+k)O(n + k).

Solution (Python)

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

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.