Setter:Teja Vardhan Reddy
Editorialist: Utkarsh Pandey
DIFFICULTY:
EASY
PRE-REQUISITES:
Observation, Binomial Coefficients.
PROBLEM:
Given a sequence A1,A2,…,AN. Find the number of subsequences in which the median of the subsequence is present in that subsequence, modulo 1000000007.
Explanation
Since we have to sort each subsequence to find its median, we will sort our original sequence (i.e. A1,A2,…,AN) at first itself. The ordering won't change.
For example, the good (desired) subsequences in the sequence [9,5,7,6,8,1] are same as that in the sequence [1,5,6,7,8,9].
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:
It is clearly written in this subtask: A is a permutation of integers 1 through N. Also, the number of elements in the sequence A is N which means each element from 1 to N is present once and only once. This tells us that there is no repetition of any number. So, no subsequence having even length will be counted.
So, answer to this subtask = Number of subsequences having odd length
= Number of ways of selecting 1 out of n elements + Number of ways of selecting 3 out of n elements + Number of ways of selecting 5 out of n elements + ............. + Number of ways of selecting n or (n-1) out of n elements
$=nC1 + nC3 + nC5 + ..... + nCn$ (if 'n' is odd) or $nC1 + nC3 + nC5 + ..... + nC(n-1)$ (if 'n' is even)
where nCx denotes the number of ways of choosing x elements out of n elements
= 2^(n-1) See the proof below if you didn't understand this completely Proof:
Second and Third Subtask:
The tricky part of this question is when the elements in the sequence starts repeating. In this case, we have to find the number of subsequences having even length.
To find the subsequences of even length to be a good subsequence, we traverse through each element which is repeated at least once. For this element to be our median element in one of the subsequences of even length, we must have at least k elements at its index's left and we must have at least k elements at the other element index's right (Note that our array was sorted), where k is a non-negative integer.
An example is provided in the section below in case you didn't understand the lines written above:
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