Alternative Permutations

1부터 n까지의 레이블로 만든 이진 탐색 트리 중 생성 순열의 개수가 정확히 k개인 가장 작은 n을 5000 이하에서 찾고, 그런 트리를 만드는 사전순 최소 순열을 출력한다.

어려움8트리조합론동적 계획법아직 제출이 없습니다시간 제한8초메모리 제한256 MB

문제

Johnny wrote a permutation to binary search trees (BST) converter: given a permutation (π_1,π_2,,π_n)(\pi\_1, \pi\_2, \ldots, \pi\_n) it assigns π_1\pi\_1 to the root of the BST, from numbers in (π_2,π_3,,π_n)(\pi\_2, \pi\_3, \ldots, \pi\_n) smaller than π_1\pi\_1 (in the same order!) it recursively creates a BST and attaches it as a left subtree of the root; symmetrically, from numbers in (π_2,π_3,,π_n)(\pi\_2, \pi\_3, \ldots, \pi\_n) larger than π_1\pi\_1 it also creates a BST and attaches it as a right subtree of th root.

To Johnny's surprise, it turns out that different permutations can result in the same BST -- for instance the permutations (2,3,1)(2, 3, 1) and (2,1,3)(2, 1, 3) result in the same BST. He found this fact astonishing and immediately defined Johnny's Numbers J_kJ\_k: the kk-th Johnny's Number is the smallest nn such that  there is a BST on nn nodes labelled with numbers 1,2,,n1, 2, \ldots, n, that can be obtained from exactly kk different permutations of the numbers  1,2,,n1, 2, \ldots, n.

The investigation of Johnny's Numbers is difficult and their popularity is decreasing. Help Johnny out--compute Johnny's Number J_kJ\_k for the given kk.

입력

The first line of input consists a single natural number kk (1k10111\le k \le 10^{11}).

출력

In the first line of the input print a single positive integer:  kk-th Johnny's Number J_kJ\_k, assuming that it exists and it is at most 5,0005\\,000. Second line of the input must contain J_kJ\_k integers --- lexicographically minimal generating  permutation between kk ones.

Otherwise, that is if J_kJ\_k does not exists or it is larger than 5,0005\\,000 you should write the word "NIE" (Polish for 'no').

힌트

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).