A lot of combinatorial problems have the form of n!/(m!*p!*q!...) for the number of distinct permutations. Is there any efficient algorithm to enumerate all distinct permutations ?
Let’s use an example to clear things up. Let’s say that 10 persons want to play a 5v5 game. In order to help them build a matchup (ie who goes in each team), we would like to enumerate all possible matchups. How many matchups are there ? Overall, there is 10! = 3628800 possible ways of arranging the players. However, swapping players inside team 1 does not change anything (A+B+C+D+E vs F+G+H+I+J is the same as B+A+C+D+E vs F+G+H+I+J), so we must divide this number by 5!. Same for swapping players inside team 2. Swapping team 1 and team 2 results to the same matchup, so we want to divide the final result by 2. In the end, there is 10!/(5!*5!*2!)=126 distinct matchups. Is there any way to find them ?
The naive algorithm would be to enumerate all players permutations, and only return "canonical" representation of a matchup (for example, inside a team players must be ordered in the lexicographical order). That is however awfully inefficient ; for a 8 vs 8 game, we must enumerate and evaluate 16! (more than 20 trillions) permutations in order to enumerate the 6435 distinct matchups. Is there any non-naive alternative ?