#B4322. Square Partition Conversion

    ID: 11978 Type: Default 1000ms 256MiB

Square Partition Conversion

Square Partition Conversion

Keko has invented a new method to partition a square. Initially, there is a square which we call the round 00 figure. In the first round (round 11), the square is divided evenly into 4 smaller squares: top-left, top-right, bottom-left, and bottom-right. They are labeled as A\tt{A}, B\tt{B}, C\tt{C}, and D\tt{D} respectively.

For every subsequent round, each square from the previous round is divided into 4 parts following the same pattern. In round 22, for example, the four parts of the square originally labeled as A\tt{A} will be labeled AA\tt{AA}, AB\tt{AB}, AC\tt{AC}, and AD\tt{AD} respectively (where the second letter represents the division within that square). In general, at round nn, each cell is identified by a string of length nn composed of the characters {A, B, C, D}.

The mapping from letter to position within a sub-square is defined as follows (each sub-square is a 2×22\times2 grid):

  • A\tt{A}: top-left cell (row offset 0, column offset 0)
  • B\tt{B}: top-right cell (row offset 0, column offset 1)
  • C\tt{C}: bottom-left cell (row offset 1, column offset 0)
  • D\tt{D}: bottom-right cell (row offset 1, column offset 1)

When the entire square is subdivided for round nn, the grid size becomes 2n×2n2^n \times 2^n. Rows are numbered from top to bottom starting from 11, and columns are numbered from left to right starting from 11.

Your task is to convert between a cell's identifier (a string consisting of letters A, B, C, D) and its position (row and column numbers) in the grid.

The program should support two types of queries. Each test case begins with an integer indicating the query type:

  1. If the query type is 1, then the next token is a string representing the cell's identifier. You must output the cell's position in the format "row column" (both 1-indexed).

  2. If the query type is 2, then the next three integers are provided: nn, rr, and cc, where nn is the round number (i.e. the length of the identifier), and rr and cc (1-indexed) specify the cell's row and column in the 2n×2n2^n \times 2^n grid. You must output the corresponding cell identifier.

For example, given the identifier \tt{AB} in query type 1, the answer would be "1 2". Conversely, for query type 2 with input "2 1 2", the expected identifier is \tt{AB}.

Note: It is guaranteed that the provided row and column numbers will be valid for the specified nn.

inputFormat

The first line contains an integer TT, the number of test cases. Each of the following TT test cases begins with an integer indicating the query type.

If the query type is 1, it is followed by a string SS (without spaces) representing the cell identifier.

If the query type is 2, it is followed by three integers: nn, rr, and cc, where nn (the number of rounds), rr (the row number), and cc (the column number) describe the position in a 2n×2n2^n \times 2^n grid.

All tokens are separated by whitespace.

outputFormat

For each test case, output a single line:

  • If the query type is 1, output two integers separated by a space: the row and column (both 1-indexed) corresponding to the given identifier.
  • If the query type is 2, output the cell's identifier (a string consisting of exactly nn characters from the set {A, B, C, D}).

sample

3
1 AB
2 2 1 2
1 D
1 2

AB 2 2

</p>