PROBLEM LINK:Author: Diwakar Bhardwaj DIFFICULTY:EASYMEDIUM PREREQUISITES:Dynamic Programming, Permutation & Combination, Inverse Modulo PROBLEM:For given numbers N, I and K, we have to find the number of sorted subsets where I comes at position K that can be formed by set{1,2,...N}. EXPLANATION:Let's fix the position of $I_{th}$ number at $K_{th}$ position as given in the problem. Now, we have to find total number of possible subsets that can be formed with given conditions. Firstly, choose the numbers for the first $K1$ positions as they are blank right now. We have to fill these positions with first $I1$ numbers as we have to keep the subset in sorted form. So, total number of ways for choosing the number from $I1$ numbers for $K1$ positions is $^{(I1)}C_{(K1)}$. Now, for the remaining numbers from $I+1$ to $N$, it is not necessary that they appear in the subset or not. So, for every number greater than $I$, either the number appears in subset or not. So, permutation for either choosing each number or not is $2$ and for total $NI$ numbers it will be $2^{(NI)}$. Hence, total number of required subsets are $^{(I1)}C_{(K1)} * 2^{(NI)}$. We have to give output as $(^{(I1)}C_{(K1)} * 2^{(NI)})\%(10^9+7)$. Now, we just have to calculate $^{I1}C_{K1}$ for each query (keeping the number of queries $10^6$ into consideration); the best solution is to pre compute each and every case of $^{I1}C_{K1}$. For, pre computation we can use the formula for computing $^nc_r$: $$^nC_r = ^{n1}C_{r1} + ^{r1}C_r$$. OPTIMAL COMPLEXITY:$\mathcal{O}(N*K + Q)$ AUTHOR'S AND TESTER'S SOLUTIONS:Author's solution can be found here.
This question is marked "community wiki".
asked 30 Mar '16, 00:19
