Decorator Cubelover
Time limit1sMemory limit512 MB
Arrange n decorations on an n^3-cycle so each window of three labels is unique and the positional-value sum is minimal; report the nth digit of p-1.
- Level
Hard9 of 10
- Topics
- Combinatorics, Math, Greedy, Number theory
- Solved
- No attempts yet
Problem
Kipa loves perfect cubes more than anything. Mori, who knew this well, gave Kipa a large round cake cut into identical pieces as a birthday present. Every piece tastes different. There is one problem: the cake is so large that to eat it you have to climb on top of the pieces!
Standing on a cake piece, Kipa has poor eyesight and can see only the current piece and the pieces on either side. Kipa first wants to taste every piece in clockwise order and place one of decorations, numbered 1 through , so that no matter where Kipa stands, the current piece can be identified. Counting exactly pieces by eye is hard.
Kipa starts standing on a particular piece with fatigue 0 and tentative fatigue 1. Each time Kipa skips one piece clockwise, the tentative fatigue is multiplied by . Also, because each piece is so wide, a piece holds only one decoration, decoration , and placing it adds times the tentative fatigue to the fatigue.
Write a program that helps Kipa: when the decorations are arranged so that the fatigue is minimized after placing all decorations and walking one full lap back to the starting position, output which decoration is placed on the -th piece clockwise from the starting position.
Input
The first line gives the number of test cases . is a positive integer at most .
The next lines each give a positive integer at most and a positive integer at most , separated by a space.
Output
For each of the lines, output the number of the decoration placed on the -th piece clockwise from the starting position.
Hint
There are 2 decorations in total, and there are 16 ways to place them so that every piece is distinguishable.
Among them, placing them in the order 2, 2, 2, 1, 2, 1, 1, 1 gives the smallest final fatigue, 278.