A ternary sequence is a sequence of digits made up entirely of 0's, 1's, and 2's such as 002221102. (this is also known as a base three number)

A. How many 3-digit ternary sequences have exactly one 1?

B. How many 4-digit ternary sequences have exactly one 1?

C. How many 4-digit ternary sequences have exactly two 1?

D. How many n-digit ternary sequences have exactly k 1's (where n and k are non negative ints with k < n?

For a b and c my answers are

A. 12

B. 32

C. 12

I did it by hand but was wondering if there is another way to do this with equations? and i do not know how to do D. Can someone explain please.