#C8984. Distinct Words Formation
Distinct Words Formation
Distinct Words Formation
You are given n cards, each with a lowercase letter, and you must form words by selecting exactly m of these cards. The order of the cards matters, so two words that differ in the order of letters are considered different.
Your task is to count the number of distinct words that can be formed. Note that some cards may have the same letter, so duplicate words might be generated if you are not careful. Formally, if the set of letters is \(L = \{l_1, l_2, \dots, l_n\}\), you need to find the number of unique sequences of length m chosen without replacement from L.
Example:
If n = 3, m = 2, and the cards are [a, b, c], then the distinct words are: ab, ac, ba, bc, ca, cb. Hence, the answer is 6.
inputFormat
The input is given via standard input (stdin) and consists of two lines:
- The first line contains two integers n and m separated by a space, where n is the total number of cards and m is the number of cards used to form each word.
- The second line contains n space-separated lowercase letters, representing the letters on the cards.
outputFormat
Output a single integer to standard output (stdout) -- the number of distinct words that can be formed using exactly m cards.
## sample3 2
a b c
6