Tower of noiHa

시간 제한1초메모리 제한1024 MB

요약
루카스가 k번의 최적 이동을 한 뒤 아들이 모든 원판을 1번 기둥에서 3번 기둥으로 한 번에 옮긴 상태에서, 목표 상태까지 필요한 최소 유효 이동 횟수를 구한다.
난이도

어려움10점 중 8점

유형
그리디, 재귀, 수학, 구현
정답자
아직 제출이 없습니다

문제

Lucas believes that at six years old, his son is ready to learn some basic algorithms. To start, he chose one of the most beautiful techniques: recursion, and to illustrate it, he picked the wellknown recursion game: the Tower of Hanoi.

The Tower of Hanoi is a mathematical game consisting of three rods and a number of disks of various diameters, which can slide onto any rod. The puzzle begins with the disks stacked on the first rod in order of size, the smallest at the top, thus approximating a conical shape. The objective of the puzzle is to transfer the entire stack to the last rod, obeying the following rules:

  • Only one disk may be moved at a time.
  • Each move consists of taking the top disk from one of the stacks and placing it on top of another stack or on an empty rod.
  • No disk may be placed on top of a disk smaller than itself.

Lucas knows that the minimal number of moves required to solve a Tower of Hanoi puzzle is 2n−12^n - 1, where nn is the number of disks. What’s more, the optimal moves are unique, which means that nn and the number of moves that have been done uniquely represent the current state of the game, given that the disks are always moved optimally.

Lucas was showing his son how to solve the game step by step. He has already done the first kk optimal moves. Since it will still take a while to finish, he took a short break to grab some snacks. Unfortunately, when he came back, he found that his naughty little son has done a big “move”: knowing that the goal is to transfer all disks from the first rod to the last rod, his son literally transferred “all disks from the first rod to the last rod” in one move (without changing their respective order), see figure K.1.

Figure K.1: Layout of the game before and after the son’s big “move”.

Lucas believes that he can still use this as a teaching opportunity. He decides to solve the game still using only “valid” moves. However, he wonders what is the current minimum number of moves required to solve the game. Since he is also busy dealing with his son, he needs your help!

Note that a “valid” move is still well-defined even if the given state is invalid. That is, you can only move one top disk at a time, and you cannot place it on top of another disk that is smaller than it. In particular, it is valid to put a disk of size aa on top of a rod that contains a disk of size bb (b<ab < a) if the top disk on this rod has size cc (a<ca < c).

입력

The first line contains an integer nn (1≤n≤200,0001 ≤ n ≤ 200\\, 000), the number of disks in the game.

The second line contains an integer kk (0≤k≤2n−10 ≤ k ≤ 2^n - 1), the number of optimal moves Lucas did prior to the big move. Note that kk is given in binary

출력

Output one integer in binary, the minimum number of moves required to finish the game.

예제3

  1. 예제 1

    입력
    3
    0
    
    예상 출력
    0
    
  2. 예제 2

    입력
    3
    10
    
    예상 출력
    110
    
  3. 예제 3

    입력
    5
    11011
    
    예상 출력
    11