#B4102. Magical Cloning Machine
Magical Cloning Machine
Magical Cloning Machine
There is a magical cloning machine that can clone anything. When you place a sample into the machine, it produces an exact copy of the sample.
Xiao Ming has obtained k precious plant seeds, represented by A, B, C, D, ..., Z (with 1 ≤ k ≤ 26). Initially, there is exactly one seed of each type.
He arranges these k seeds in a queue in alphabetical order. Then he repeats the following cloning process:
- Take the first seed in the queue and place it into the cloning machine.
- The machine produces one cloned seed identical to the sample.
- Append both the original seed (taken from the front) and its clone (in that order) to the end of the queue.
For example, if k = 7, the seeds are labeled as A, B, C, D, E, F, G. The process runs as follows:
- Before processing the 1st seed, the queue is: A, B, C, D, E, F, G.
- After processing seed A, the queue becomes: B, C, D, E, F, G, A, A.
- Before processing the 3rd seed, the queue is: C, D, E, F, G, A, A, B, B.
- After processing seed C, the queue becomes: D, E, F, G, A, A, B, B, C, C.
Your task is: given k and n, determine which seed (represented by a letter from A to Z) is processed as the nth seed in this infinite cloning sequence.
Hint: Notice that the process has a doubling effect. In each round, every seed appears consecutively a number of times that doubles in each round. Mathematically, you may find r such that
\( n > k \cdot 2^{r} \) and then compute the answer using index = \(\left\lfloor \frac{n - \text{offset} - 1}{2^{r}} \right\rfloor\), where \( \text{offset} = k(2^{r} - 1) \).
inputFormat
The input consists of two space-separated integers k and n. Here, k (1 ≤ k ≤ 26) represents the number of distinct seeds and n (a positive integer) represents the position of the seed to be processed.
outputFormat
Output a single letter (from A to Z) that corresponds to the seed processed as the nth seed.
sample
3 4
A