#P1243. Find the k-th Lexicographically Ordered Subset
Find the k-th Lexicographically Ordered Subset
Find the k-th Lexicographically Ordered Subset
Given a set ( N = {1,2,\cdots,n} ), consider all of its subsets. We define a "less than" relation between any two subsets as follows:
For two subsets ( S_1 = {X_1,X_2,\cdots,X_i} ) (with ( X_1 < X_2 < \cdots < X_i )) and ( S_2 = {Y_1,Y_2,\cdots,Y_j} ) (with ( Y_1 < Y_2 < \cdots < Y_j )), if there exists an integer ( k ) ( (0 \le k \le \min(i,j)) ) such that ( X_1 = Y_1, \cdots, X_k = Y_k ) and either ( k = i ) or ( X_{k+1} < Y_{k+1} ), then we say ( S_1 ) is less than ( S_2 ).
Notice that under this ordering, the empty set is the smallest subset. Your task is: given two integers ( n ) ( (n \le 31) ) and ( k ) ( (k < 2^n) ), output the ( k )-th smallest subset (using 0-indexing) when all subsets of ( N ) are arranged in the order defined above. The elements in the output subset must be printed in increasing order, separated by spaces.
Note on Ordering: The ordering compares the elements of the subset as an increasing sequence. If one subset is a prefix of another, the shorter (prefix) set is considered smaller.
inputFormat
The input consists of two integers. The first integer n (\(1 \le n \le 31\)) denotes the size of the set \(N = \{1, 2, \cdots, n\}\). The second integer k (\(0 \le k < 2^n\)) denotes the index of the subset to output in the lexicographical order defined above. Note that the indexing is 0-indexed: the 0-th subset is the empty set.
outputFormat
Output the elements of the \( k \)-th smallest subset in increasing order, separated by a single space. If the subset is empty, output an empty line.
sample
3
0