Permutations and cycles (1)

Write a program to print all the permutations of {1,,n}\{1, \ldots, n\} with exactly kk cycles, where 1kn1 \le k \le n. For exemple, consider the permutation (4,3,2,5,1,7,6)(4, 3, 2, 5, 1, 7, 6). At position 11 there is a 44, at position 44 there is a 55, and at position 55 there is a 11. Therefore, one of the cycles is 14511 \rightarrow 4 \rightarrow 5 \rightarrow 1. The other two cycles are 2322 \rightarrow 3 \rightarrow 2 and 6766 \rightarrow 7 \rightarrow 6. The permutation (3,2,1)(3,2,1) has the two cycles 1311 \rightarrow 3 \rightarrow 1 and 222 \rightarrow 2, and the permutation (3,4,5,6,7,1,2)(3,4,5,6,7,1,2) only has the cycle 135724611 \rightarrow 3 \rightarrow 5 \rightarrow 7 \rightarrow 2 \rightarrow 4 \rightarrow 6 \rightarrow 1.

Input

Input consists of nn and kk, with 1kn1 \le k \le n.

Output

Print all the permutations of {1,,n}\{1, \ldots, n\} with kk cycles.

Information about the checker

You can print the solutions to this exercise in any order.

Hint

A possible program does not build the permutations consecutively from left to right, but jumping over the solution, using a function

    void f(int i, int ini, int cells, int cycles);

where @i@ is the next cell to fill, @ini@ is where the current cycle—still to be closed—starts, @cells@ is the number of cells still free, and @cycles@ is the number of cycles yet to be created.

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T12:03:59.828Z

© Jutge.org, 2006–2026.
https://jutge.org