Magic Orbs
Time limit2sMemory limit128 MB
Construct a permutation of rooms 1..N starting at a given M so that consecutive absolute differences use each value 1..N-1 exactly once, visiting every room once.
- Level
Hard8 of 10
- Topics
- Combinatorics, Math, Backtracking
- Solved
- No attempts yet
Problem
A wizard wants to collect as many treasures as possible from a line of N = 2^m rooms. The rooms are numbered from 1 to N, and each room contains one treasure. There are no doors between rooms, so the only way to enter or move between rooms is by using magic orbs.
The wizard has exactly one IN orb, one OUT orb, and one orb A_k for each 1 <= k <= N-1.
INmust be used first. It places the wizard in one room. The wizard cannot know that room in advance, but after entering, the room numberMis known.A_kmoves the wizard exactlykrooms left or exactlykrooms right. The wizard chooses the direction, but the destination must still be between rooms1andN.- Each orb can be used at most once.
OUTmust be used last to leave the castle.
Given N and the initial room M, print a visiting order that lets the wizard collect the maximum possible number of treasures. For the given constraints, it is possible to visit every room exactly once.
Input
The first line contains the integer N (2^1 <= N <= 2^13 = 8192). N is a power of two.
The second line contains the integer M (1 <= M <= N), the room entered by using IN.
Output
Starting with M, print N room numbers in visiting order on one line. Put one space between adjacent room numbers.
For consecutive printed rooms, the absolute differences must be exactly the numbers 1, 2, ..., N-1 in some order. If there is more than one valid answer, print any one of them.