PROBLEM LINK:
Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4
Setter: Utkarsh Gupta
Tester: Nishank Suresh, Utkarsh Gupta
Editorialist: Pratiyush Mishra
DIFFICULTY:
2906
PREREQUISITES:
None
PROBLEM:
You are given 2 integers N and M. You also have an empty set S.
Consider an array A of length N such that 1 \leq A_i \leq M for each 1 \leq i \leq N (there are M^N such arrays in total).
For each of these M^N arrays, insert all of its longest non-decreasing subsequences into S.
Output the size of S. Since the size of S might be large, output it modulo 10^9+7.
Note that since S is a set, it stores only distinct values. For example, when N = 3 and M = 2, the sequence [1, 1] is a longest non-decreasing subsequence for both A = [1, 2, 1] and for A = [2, 1, 1] — however, it will only be present once in S.
EXPLANATION:
Observation: All the non-decreasing sequences greater than or equal to the length of \lceil \frac{n}{m} \rceil belongs to the set.
Here since we only have m numbers so some number would atleast appear \lceil \frac{n}{m} \rceil times so minimum length of non-decreasing sequences would be \lceil \frac{n}{m} \rceil.
Hence our answer would be the count of all non-decreasing sequences of length \geq \lceil \frac{n}{m} \rceil since some element would appear with frequency \geq \lceil \frac{n}{m} \rceil, thus given any LIS of length greater than or equal to this, it’s possible to construct a full sequence.
Thus to count the number of non decreasing sequences, we just need to fix the frequencies of each element. In order to do this we can represent this as a multiplication of polynomials.
Here in the final product of the above expression, the coefficient of say x^j, would denote the number of non-decreasing subsequences of j length.
So our answer would be sum of coefficients of x^y,x^{y+1}....x^n, where y = \lceil \frac{n}{m} \rceil
Now coefficient of x^i in (1-x)^{-m} is:
Thus our answer is
In order to solve this we use the following property of combinations:
Now adding and subtracting \binom{m+y-1}{m}, we get
Using property above repeatedly ,we get our final answer as:
TIME COMPLEXITY:
O(log(N+M)), for each test case.
SOLUTION:
Editorialist’s Solution
Setter’s Solution
Tester1’s Solution
Tester2’s Solution