Under construction

Still being built. Things may change

Alternative Permutations

Time limit8sMemory limit256 MB

Summary
Given k, find the smallest n for which some BST on 1 to n is produced by exactly k permutations, and print the lexicographically smallest one.
Level

Hard9 of 10

Topics
Combinatorics, Tree, Dynamic programming, Recursion
Solved
No attempts yet

Problem

Johnny wrote a permutation to binary search tree (BST) converter. Given a permutation (π1,π2,…,πn)(\pi_1, \pi_2, \ldots, \pi_n), it assigns π1\pi_1 to the root. From the numbers in (π2,π3,…,πn)(\pi_2, \pi_3, \ldots, \pi_n) that are smaller than π1\pi_1, it recursively builds a BST, keeping their original order, and attaches it as the left subtree of the root. Symmetrically, from the numbers larger than π1\pi_1, it builds a BST and attaches it as the right subtree. To Johnny's surprise, different permutations can produce the same BST. For instance, (2,3,1)(2, 3, 1) and (2,1,3)(2, 1, 3) produce the same BST. Surprised by this fact, he defined Johnny's Numbers JkJ_k: the kk-th Johnny's Number is the smallest nn such that some BST on nn nodes labelled 1,2,…,n1, 2, \ldots, n can be obtained from exactly kk different permutations of 1,2,…,n1, 2, \ldots, n. Studying Johnny's Numbers is hard, and interest in them is fading. Help Johnny compute JkJ_k for a given kk.

Input

The first line contains a natural number kk (1≤k≤10111 \le k \le 10^{11}).

Output

On the first line, print a positive integer JkJ_k, provided that it exists and is at most 50005000. On the second line, print the JkJ_k integers of the lexicographically smallest generating permutation among the kk permutations. Otherwise, that is, if JkJ_k does not exist or is larger than 50005000, print the word NIE (Polish for 'no').

Hint

The tree having exactly eight generating permutations is shown below: All permutations generating that tree are: (2,1,4,3,5)(2, 1, 4, 3, 5), (2,1,4,5,3)(2, 1, 4, 5, 3), (2,4,1,3,5)(2, 4, 1, 3, 5), (2,4,1,5,3)(2, 4, 1, 5, 3), (2,4,3,1,5)(2, 4, 3, 1, 5), (2,4,3,5,1)(2, 4, 3, 5, 1), (2,4,5,1,3)(2, 4, 5, 1, 3), (2,4,5,3,1)(2, 4, 5, 3, 1).

Examples1

  1. Example 1

    Input
    8
    
    Expected output
    5
    2 1 4 3 5