You are not logged in. Please login at www.codechef.com to post your questions!

×

CDVA1607 - Editorial

PROBLEM LINK:

Practice
Contest

Author: Diwakar Bhardwaj
Tester: Aditya Kumar
Editorialist: Shubham Kabra

DIFFICULTY:

EASY-MEDIUM

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 $K-1$ positions as they are blank right now. We have to fill these positions with first $I-1$ numbers as we have to keep the subset in sorted form. So, total number of ways for choosing the number from $I-1$ numbers for $K-1$ positions is $^{(I-1)}C_{(K-1)}$.

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 $N-I$ numbers it will be $2^{(N-I)}$.

Hence, total number of required subsets are $^{(I-1)}C_{(K-1)} * 2^{(N-I)}$.

We have to give output as $(^{(I-1)}C_{(K-1)} * 2^{(N-I)})\%(10^9+7)$.

Now, we just have to calculate $^{I-1}C_{K-1}$ for each query (keeping the number of queries $10^6$ into consideration); the best solution is to pre compute each and every case of $^{I-1}C_{K-1}$. For, pre computation we can use the formula for computing $^nc_r$: $$^nC_r = ^{n-1}C_{r-1} + ^{r-1}C_r$$.

OPTIMAL COMPLEXITY:

$\mathcal{O}(N*K + Q)$

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.
Tester's solution can be found here.

This question is marked "community wiki".

asked 30 Mar '16, 00:19

suraj_ch77's gravatar image

5★suraj_ch77
513
accept rate: 33%

edited 30 Mar '16, 14:11

admin's gravatar image

0★admin ♦♦
19.8k350498541

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,639
×2,167
×1,672
×886
×342
×34
×5

question asked: 30 Mar '16, 00:19

question was seen: 854 times

last updated: 30 Mar '16, 14:11