#C13699. Subsets Generation

    ID: 43265 Type: Default 1000ms 256MiB

Subsets Generation

Subsets Generation

Given a list of distinct integers, you are to generate all possible subsets in non‐descending order. There are two modes for this problem:

  1. Mode 1: Generate all subsets. These subsets must be sorted first by their length and then in lexicographical order.

  2. Mode 2: Generate only the subsets that have exactly kk elements. The subsets should be output in non‐descending order as well.

Note: In both modes, the input list of integers should be sorted in non‐descending order before processing. The mathematical formulation for a subset S{a1,a2,,an}S \subseteq \{a_1, a_2, \dots, a_n\} is such that if S={s1,s2,,sm}S = \{s_1, s_2, \dots, s_m\} then s1s2sms_1 \le s_2 \le \cdots \le s_m. This ordering is maintained in the output.

inputFormat

Input is read from standard input (stdin) and its format depends on the mode:

  • The first line contains a single integer indicating the mode. If the mode is 1, then the problem requires generating all subsets. If the mode is 2, then the problem requires generating only those subsets of length kk.

  • For mode 1:

    • The second line contains an integer nn, the number of elements in the list.
    • The third line contains nn space-separated integers.
  • For mode 2:

    • The second line contains an integer nn, the number of elements in the list.
    • The third line contains nn space-separated integers.
    • The fourth line contains the integer kk indicating the desired length of each subset.

outputFormat

The output should be printed to standard output (stdout) as a single line string representing a Python-style list of lists.

  • For mode 1, print the list of all subsets sorted by length and lexicographically.
  • For mode 2, print the list of all subsets that have exactly kk elements.

Each subset is printed in the format [a, b, ...].## sample

1
3
1 2 3
[[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]