4002. Count Valid Sequences
Description
You are given two positive integers n and k.
A valid sequence is a sequence of k positive integers such that:
- The sum of all integers in the sequence is equal to
n. - The product of all integers in the sequence is even.
Return the number of valid sequences. Since the answer may be very large, return it modulo 109 + 7.
Two sequences are considered different if they differ at any index. For example, [1, 1, 2] and [1, 2, 1] are considered different sequences.
Example 1:
Input: n = 5, k = 3
Output: 3
Explanation:
The sequences of length k = 3 whose sum is 5 are:
| Sequence | Product | Parity |
|---|---|---|
[1, 1, 3] | 1 * 1 * 3 = 3 | Odd |
[1, 2, 2] | 1 * 2 * 2 = 4 | Even |
[2, 1, 2] | 2 * 1 * 2 = 4 | Even |
[2, 2, 1] | 2 * 2 * 1 = 4 | Even |
[1, 3, 1] | 1 * 3 * 1 = 3 | Odd |
[3, 1, 1] | 3 * 1 * 1 = 3 | Odd |
There are 3 sequences with an even product, thus the answer is 3.
Example 2:
Input: n = 3, k = 2
Output: 2
Explanation:
The sequences of length k = 2 whose sum is 3 are:
| Sequence | Product | Parity |
|---|---|---|
[1, 2] | 1 * 2 = 2 | Even |
[2, 1] | 2 * 1 = 2 | Even |
There are 2 sequences with an even product, thus the answer is 2.
Example 3:
Input: n = 5, k = 5
Output: 0
Explanation:
The only possible sequence of length k = 5 whose sum is 5 is [1, 1, 1, 1, 1], which has an odd product. Thus, the answer is 0.
Constraints:
1 <= n <= 5 * 1051 <= k <= n
Solutions
Solution 1: Combinatorics
Thinking
The number of compositions of \(n\) into \(k\) positive parts is \(\binom{n-1}{k-1}\). The product is even if and only if at least one part is even, so we subtract the all-odd compositions from the total.
All-odd compositions exist only when \(n\) and \(k\) have the same parity. Substituting \(2a_i+1\) yields \(\sum a_i=(n-k)/2\), i.e. \(\binom{(n+k)/2-1}{k-1}\); otherwise that term is \(0\).
With \(n\) and \(k\) up to \(5\times 10^5\), each binomial coefficient needs precomputed factorials and inverse factorials. Queries then subtract modulo \(10^9+7\) in \(O(1)\).
The number of ordered ways to write \(n\) as a sum of \(k\) positive integers is \(\binom{n-1}{k-1}\). An even product means "at least one even number"; the complement is "all odd".
Therefore the answer is:
If every number is odd, write the \(i\)-th number as \(2a_i + 1\) (\(a_i \ge 0\)). Then:
All-odd sequences exist only when \(n\) and \(k\) have the same parity (i.e., \(n + k\) is even), and their count is \(\binom{\frac{n+k}{2}-1}{k-1}\); otherwise the count is \(0\).
After precomputing factorials and modular inverses, each combination can be evaluated in \(O(1)\). Return the answer modulo \(10^9+7\).
The time complexity is \(O(N + \log M)\) for preprocessing, and the space complexity is \(O(N)\), where \(N = 5 \times 10^5\) and \(M = 10^9+7\). Each query is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |