Logo
(追記) (追記ここまで)

3457번 - Which is Next 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB52250.000%

문제

Every computer science student knows binary trees. Here is one of many possible definitions of binary trees. Binary trees are defined inductively. A binary tree t is either an external node (leaf) しろまる or an ordered pair t = (t1, t2) representing an internal node くろまる with two subtrees attached, left subtree t1 and right subtree t2. Under this definition the number of nodes in any binary tree is odd. Given an odd integer n let B(n) denote the set of all binary trees with n nodes, both internal and external. For instance B(1) consists of only one tree しろまる, B(3) = {(しろまる,しろまる)} and B(5) = {(しろまる, (しろまる,しろまる)), ((しろまる,しろまる),しろまる)}. The trees of B(5) are depicted in the figure below.

Denote by |t| the number of nodes in a tree t. Given a tree t we define its unique integer identifier N(t) as follows:

  • N(しろまる) = 0
  • N(t1, t2) = 2|t1|+|t2| + 2|t2| · N(t1) + N(t2)

For instance, N (しろまる,しろまる) = 22 + 21 · 0 + 0 = 4, N (しろまる,(しろまる,しろまる)) = 24 + 23 · 0 + 4 = 20, N ((しろまる,しろまる),しろまる) = 24 + 21 · 4 + 0 = 24.

Consider the following linear order on all binary trees:

  • しろまる ⪯ t
  • (t1, t2) ⪯ (u1, u2) when t1 ≺ u1, or t1 = u1 and t2 ⪯ u2

In this order a single leaf is the smallest tree and given two nonleaf trees, the smaller one is that with the smaller left tree, if the left subtrees are different, and that with the smaller right subtree, otherwise. Hence for instance (しろまる,(しろまる,しろまる)) ≺ ((しろまる,しろまる),しろまる), since we have しろまる ≺ (しろまる,しろまる). Assume now that the trees in B(n) were sorted using the relation ⪯. Then, for each tree t in B(n) we define the successor of t as the tree that immediately follows t in B(n). If t is the largest one in B(n) then the successor of t is the smallest tree in set B(n). For instance, the successor of (しろまる,しろまる) in B(3) is the same tree (しろまる,しろまる) and the successor of (しろまる,(しろまる,しろまる)) in B(5) is ((しろまる,しろまる),しろまる).

Given the integer identifier of some tree t can you give the identifier of the successor of t in B(|t|)?

Write a program that:

  • reads the identifier of some binary tree t,
  • computes the identifier of the successor of t in B(|t|),
  • writes the result.

입력

The first and only line of the input contains one integer n (0 ≤ n ≤ 230) - the identifier of some binary tree

출력

The first and only line of the output should contain one integer s - the identifier of the successor of t in B(|t|).

제한

예제 입력 1

20

예제 출력 1

24

힌트

출처

ICPC > Regionals > Europe > Central European Regional Contest > CERC 2003 G번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /