#P15145. [SWERC 2025] Keygen 3

[SWERC 2025] Keygen 3

题目描述

Luke has just discovered a new 5D kart videogame. However, the software is not free: in order to activate it, you need to provide a license key.

Luke has found out that, in order to be valid, a license key must be a permutation of length nn with both of the following properties:

  • it is bitonic;
  • it has mm cycles.

Let kk denote the number of license keys (i.e., the number of permutations satisfying the above conditions). Luke is an altruistic hacker, so he wants to provide distinct license keys for all his 2000 friends. However, kk might be less than 2000.

Help Luke by printing r=min(k,2000)r = \min(k, 2000) distinct valid license keys.

A permutation of length nn is an array consisting of nn distinct integers from 1 to nn in arbitrary order. For example, [2,3,1,5,4][2, 3, 1, 5, 4] is a permutation, but [1,2,2][1, 2, 2] is not a permutation (2 appears twice in the array), and [1,3,4][1, 3, 4] is also not a permutation (n=3n = 3 but there is 4 in the array).

A permutation p1,p2,,pnp_1, p_2, \ldots, p_n is bitonic if there exists an index ii (1in1 \le i \le n) such that

  • pj1pjp_{j-1} \le p_j for 2ji2 \le j \le i;
  • pjpj+1p_j \ge p_{j+1} for ijn1i \le j \le n-1.

A cycle is a subset C{1,2,,n}C \subseteq \{1, 2, \ldots, n\} such that

  • CC is non-empty;
  • if xCx \in C, then pxCp_x \in C;
  • CC is minimal, i.e., there does not exist a cycle CC' such that CCC' \subsetneq C.

输入格式

The input consists of a single line containing two integers n,mn, m (1mn1001 \le m \le n \le 100) — the length of the permutations and the target number of cycles.

输出格式

In the first line, print a single integer rr: the number of permutations you are going to print. Note that rr must be min(k,2000)\min(k, 2000) as defined in the statement.

Then, print rr lines. Each line must contain a bitonic permutation of length nn, with mm cycles. The permutations must be distinct.

6 3
9
1 4 5 6 3 2
6 5 4 3 2 1
1 2 4 5 6 3
1 2 5 6 4 3
1 3 4 6 5 2
1 5 6 4 3 2
3 5 6 4 2 1
1 3 6 5 4 2
2 6 5 4 3 1

提示

Explanation of sample 1.

In the example, there are 9 valid license keys (i.e., bitonic permutations of length 6, with 3 cycles). For example, [3,5,6,4,2,1][3, 5, 6, 4, 2, 1] is bitonic (in the above definition, i=3i = 3), and it has 3 cycles: {1,3,6}\{1, 3, 6\}, {2,5}\{2, 5\}, {4}\{4\}. So you have to print r=min(9,2000)=9r = \min(9, 2000) = 9 such permutations.