Setter:Teja Vardhan Reddy
DIFFICULTY: PREREQUISITES: PROBLEM: Explanation The subsequences having odd length will definitely qualify to be one of our good subsequence (as in the subsequences with odd length, the element at the middle position is the median of the subsequence itself). The subsequences with even length will qualify as good subsequence if and only if the middle two elements are same. It is so because the array is already sorted. View the content below in case you didn't understand this statement. So our answer to this problem is : (Number of subsequences having odd length) + (Number of good subsequences having even length with both the middle elements equal) First Subtask: So, answer to this subtask = Number of subsequences having odd length $=nC1 + nC3 + nC5 + ..... + nCn$ (if 'n' is odd) or $nC1 + nC3 + nC5 + ..... + nC(n1)$ (if 'n' is even) = 2^(n1) See the proof below if you didn't understand this completely Proof: Second and Third Subtask: Now, Calculating nCx in each iteration, we may have difficulties passing the time limit provided in the problem. To reduce the time complexity to pass the given time limit, we have to observe the given fact: Let the repeating elements be found at index i and j Suppose, there are m elements at the left of the index i and n elements at the right of index j. So, number of good subsequences: 1 (good subsequence of length 2) + $mC1 * nC1$ (good subsequences of length 4) + $mC2 * nC2$ (good subsequences of length 6) + ........... + $mCmin(m,n) * nC(min(m,n))$. By Vandermonde's identity we can sum up this value as (m+n)C(min(m,n)), details of which can be found here :Vandermonde's Identity. Bonus: Calculating queries of the type nCr % 1000000007 can be solved by precalculating $nCr % 1000000007$ upto n = whatever you need :p . Details of this can be found here: nCr % p in O(1) Time Complexity = O($N^2$) (Enough to pass all the subtasks) Editorialist's Solution: https://www.codechef.com/viewsolution/21611480 asked 12 Nov '18, 15:48

Using a proof of Vandermonde's identity to illustrate how to pick all the balanced enclosing sequences around a selected middle pair: Suppose the chosen two central elements are at positions in the sorted array with $s$ elements before the first one and $t$ elements after the second. Then consider any selection of the same number of elements from each side, say $j$ elements from each side. Now invert the selection in the "before" section, so a previously selected item becomes unselected and vice versa. This now gives $s{}j$ elements on that side. Combining this with the "after" section, you have a total of $(s{}j) + j = s$ elements from the total pool, no matter what $j$ is. You can easily see that every balanced selection can be turned into a corresponding choice of $s$ elements in this way, and vice versa  every choice of $s$ elements can be turned into a distinct balanced enclosing choice. So the number of options for any one central pair is $\binom{s+t}{s}$. example:
chosen central pair h & j ($s=7, t=4$)
choose say three from each side:
invert the "before"s:
and we have a choice of exactly $7$ elements. And any choice of $7$ elements from the $7+4=11$ available can produce a unique balanced set:
invert the "before"s:
giving $2$ each side. If you have a run of identical elements (more than two), you can make the summation a little more efficient too. answered 14 Nov '18, 02:38

I used another binomial identitythe hockey stick identityfor an O(n^2) solution. https://www.codechef.com/viewsolution/21386816 Sums of binomial coefficients where n is changing have a closed form. There doesn't exist a closed form where k is changing. $$\sum_{i=0}^n \binom{i}{k} = \binom{n+1}{k+1}$$ $$\sum_{i=m}^n \binom{i}{k} = \binom{n+1}{k+1}  \binom{m}{k+1}$$ answered 15 Nov '18, 16:18

Vandermonde's Identity was the crux to get AC with 100 points. I submitted many solutions but got this identity after a bit google surfing work. Such a soft dismissal. xD answered 12 Nov '18, 20:48

This will (https://www.youtube.com/watch?v=aGjfSTr_0AE ) help in calculating nCr % p. answered 12 Nov '18, 21:36

(m+n)C(min(m,n)) is the same as ${m+n \choose m}={m+n \choose n}$ and it can be derived directly, without using Vandermonde's identity. We need to get the number of all combinations from $m+n$ elements, $m$ of which are on the left side and $n$  on the right side, that have equal numbers of elements on each side. Lets replace the leftside part of each combination with its complement  the set of elements on the left side that don't belong to the original combination. If an original combination had $k$ elements on each side the resulting combination will contain $(mk)+k=m$ elements. This number does not depend on $k$ and is the same for all the resulting combinations. And if we take any combination of $m$ elements and perform the same operation on its left side, we will obtain a combination with equal number of elements on each side. So we have a onetoone correspondence, and the number of original combinations is equal to the number of resulting combinations, which is ${m+n \choose m}$. Update: joffan essentially describes the same. answered 14 Nov '18, 06:08

You can use DP to find number of sequences (of all lengths) which start and end at a particular element.Then finding number of sequences which use that element for even and odd length becomes easy .https://www.codechef.com/viewsolution/21585673 answered 13 Nov '18, 10:07

Lol I wrote a brute O(N ^ 3) for each test then optimized it by precalculating an array in O(1000 ^ 3), then each test only takes O(N ^ 2). Only takes 0.3 secs. (Of course, that other thing was just 1000 ^ 3 / 6, which obviously will pass) answered 13 Nov '18, 17:25

Answer is hidden as author is suspended. Click here to view.
answered 13 Nov '18, 18:02
1
The problem with your code arises in counting the number of good subsequences of even length.
One of your failed test case will be
(13 Nov '18, 23:58)
Its late, but thanks!:)
(17 Jan, 10:01)

My approach same as editorial , but precompte nCr using dp and i think it's quite easy. This is my code: https://www.codechef.com/viewsolution/21521905 answered 13 Nov '18, 19:36

What's wrong in this solution: https://www.codechef.com/viewsolution/21437286 answered 15 Nov '18, 00:17

Guys my code does the exact same thing. Please someone look into this why it's giving WA. https://www.codechef.com/viewsolution/21437286 answered 15 Nov '18, 17:56

I gather that this solution counts [1, 2, 2, 3] twice for the sequence [1, 2, 2, 3, 3] (in the step where we choose 1 number form the 2 numbers to the right of 2's, we use 2C1 which counts both the 3's individually). But, isn't this wrong? The problem becomes a lot more difficult if we try to take into account that many numbers can be nondistinct and then the Vandermonde's identity expression is invalidated! answered 23 Nov '18, 16:22

Please add UnOfficial in the title.
@aryanc403 Done :)