# How many n-digit binary sequences contain exactly k 1s?

A) How many n-digit binary $(0,1)$ sequences contain exactly k "1"s?

B) How many n-digit ternary $(0,1,2)$ sequences contain exactly k "1"s?

I am familiar with the equations $\frac{n(n-1)}{2}$ and $\frac{n!}{k!(n-k)!}$ but I am not sure how to apply them in these questions.

-

For part A, you may think of forming an $n$-digit binary sequence as filling in $n$ slots. Each slot either gets a "0" or a "1". The number of $n$-digit binary sequences with exactly $k$ ones is the number of ways to choose $k$ slots from the $n$ slots. The slots you choose get the "1"s, the others get "0"s. So, the number of $n$-digit binary sequences with exactly $k$ ones is $n\choose k$.
Part B is a bit trickier. You can choose the $k$ slots in which to insert the "1"s as we did above. The remaining $(n-k)$ slots do not get ones; but, each of these non-one slots can be filled with either a zero or a two. Consequently, using the multiplication principle, the number of $n$-digit ternary sequences with exactly $k$ ones is \eqalign{ &\textstyle\Bigl({\text{num. of ways to choose}\atop k\text{ slots for the ones}}\Bigr) \cdot\underbrace{\Bigl({\text{num. of ways to fill}\atop \text{leftmost empty slot}}\Bigr) \cdot\Bigl({\text{num. of ways to fill}\atop \text{next leftmost empty slot}}\Bigr) \cdots\Bigl({\text{num. of ways to fill}\atop \text{rightmost empty slot}}\Bigr)}_{(n-k)\text{-terms}}\cr } Computing the above, we obtain ${n\choose k}\cdot2^{n-k}$.
Of course, in the above, we assume $k\le n$...