\[ \let\oldlor\lor \renewcommand{\lor}{\; \oldlor \;} \let\oldland\land \renewcommand{\land}{\; \oldland \;} \renewcommand{\set}[1]{\{\, #1 \,\}} \renewcommand{\given}{\,\mid\,} \renewcommand{\abs}[1]{\lvert #1 \rvert} \renewcommand{\divs}{\!\;\mid\;\!} \renewcommand{\ndivs}{\!\;\nmid\;\!} \renewcommand{\betw}[3][1]{#1 \leq #2 \leq #3} \renewcommand{\mod}[1]{\ (\mathrm{mod}\ #1)} \renewcommand{\floor}[1]{\left \lfloor #1 \right \rfloor} \renewcommand{\ceil}[1]{\left \lceil #1 \right \rceil} \renewcommand{\t}[1]{\texttt{#1}} \renewcommand{\fori}[2][i]{\text{for } #1 = 0, 1, \dots, #2} \renewcommand{\x}[1]{\text{#1}} \renewcommand\concat{\mathbin{+\mkern-10mu+}} \DeclareMathOperator*{\CONCAT}{\concat} \DeclareMathOperator*{\SCC}{\|} \]
UVa 10338 - Mischievous Children
Time limit: 3.0 seconds
UVa Difficulty rating: 2
View on UVa

Our problem statement begins with a description of a character string with each letter on a panel of its own. We are asked: how many unique ways can the panels be arranged (counting the original arrangement)? Immediately, you should be thinking: dictionaries. Not necessarily the data structure (though that will come into play later) but rather the lexicographical ordering that we associate with, say, the English dictionary. For beginners, this is a clear analogy and applies directly to our problem.

Permutations

Given a finite set of N elements, how many permutations, or possible orderings, can be produced? We will preface our mathematical definition with an example. Let's say we have the following word of length N = 3

Now in order to determine how many possible ways we can arrange the letters of ABC, consider each position in our 3-letter word as an empty space.

For the first position, we have 3 options: A, B, or C.

Now, depending on our choice in position 1, we'll have 2 letters to choose from for position 2. Regardless of our choice for the first position, we will always have N choices for position 1, N-1 choices for position 2, and so on until all positions are filled.

When all elements in the set are distinct, the number of permutations for a set of N elements is,

The distinct identifier is important, because in our problem we may have non-distinct elements (i.e., letters) in our input. In general, our input is represented as a multiset, rather than a set.

Why is this distinction important?

Consider our example above, but instead say our input is the word,

For the sake of demonstration, I'll mark the letters with numerical identifiers. Expanding all possible permutations, gives us the list of orderings:

But this isn't quite right, because the list is actually:

Removing duplicate orderings:

So actually there are only 3 possible permutations for AAB.

Multinomial Coefficient Formula

We must consider the general case of the multiset, M, where each letter appears as often as its multiplicity in M. The sum of the multiplicities in M is equal to the total length, N of the input word. Now we can rephrase our problem as follows:

Given a word, length N, represented as a finite multiset, M, what is the total number of multiset permutations

This is given by the multinomial coefficient formula: n! / ((mi)! * (mi+1)! * ... * (mM)!), for i = 1 to i = M, where M is the number of distinct elements.

Implementation

Before handling any test cases, we'll use a DP (Dynamic Programming) technique to compute and store all the factorials we'll be using later in the multinomial coefficient formula. Since each word will have at most 20 letters, we compute up to the 20th factorial and store for look up by index later.

Inside int main, compute the factorials as a preprocessing step.

Read the input and evaluate the multiplicity of each letter. Above we discussed the mathematical concept of a multiset, and while there is a C++ STL data structure named std::multiset<T> , we are going to use std::map<Key, T> . The reasoning here is mostly aesthetic, but also because it reduces the amount of code you need to write and keeps it simple.

There are many variations of combinatorics problems, and the key to differentiating problem types is practice!

Links

C++ References

UVa

230 Borrowers
543 Goldbach's Conjecture
900 Brick Wall Patterns
10047 The Monocycle
10140 Prime Distance
10165 Stone Game
10338 Mischievous Children
10394 Twin Primes
10892 LCM Cardinality

CodeForces

1A Theatre Square
4A Watermelon
25A IQ test
50A Domino piling
58A Chat room
59A Word
69A Young Physicist
71A Way Too Long Words
96A Football
112A Petya and Strings
118A String Task
131A cAPS lOCK
158A Next Round
189A Cut Ribbon
230B T-primes
231A Team
263A Beautiful Matrix
281A Word Capitalization
282A Bit++
318A Even Odds
339A Helpful Maths
339B Xenia and Ringroad
455A Boredom
459B Pashmak and Flowers
474B Worms
479A Expression
486A Calculating Function
489B BerSU Ball
489C Given Length and Sum of Digits...
492B Vanya and Lanterns
500A New Year Transportation
507B Amr and Pins
513A Game
520B Two Buttons
550A Two Substrings
580A Kefa and First Steps
742A Arpa's hard exam and Mehrdad's naive cheat
766B Mahmoud and a Triangle
935A Fafa and his Company
977B Two-gram
977D Divide by three, multiply by two
996A Hit the Lottery
1097A Gennady and a Card Game
1108A Two distinct points
1154A Restoring Three Numbers