Simulate up to 60 RATS steps per data set and report the first creeper term, the first repeated term, or the last term.
Medium4SimulationStringHash mapNo attempts yetTime limit1sMemory limit256 MBThe RATS function, short for reverse add then sort, takes one decimal integer and returns a decimal integer whose digits run from the smallest to the largest.
The value is computed in three steps.
RATS(12334444): 12334444 + 44443321 = 56777765 -> 55667777
RATS(44556): 44556 + 65544 = 110100 -> 111
This problem deals with sequences whose first value is arbitrary and whose every later value is the RATS of the value before it. Here are two examples.
12334444, 55667777, 123334444, 556667777, 1233334444, 5566667777, ...
123, 444, 888, 1677, 3489, 12333, 44556, 111, 222, 444, 888, ...
The first sequence is called the creeper. It provably grows without bound in that regular pattern. A term belongs to the creeper when it reads 12, then a copies of the digit 3, then 4444, or when it reads 55, then a copies of the digit 6, then 7777, where a≥2. The second sequence falls into a cycle, and its tenth term is the first one that repeats an earlier value. Every RATS sequence is conjectured to either reach the creeper and grow without bound or cycle like the second sequence.
Given the starting value, compute the first M terms of a RATS sequence. Also decide whether the sequence repeats a value inside those M terms, or enters the creeper inside those M terms.
The first line contains the number of data sets P (1≤P≤10000). Each data set is independent and is processed the same way.
Each of the next P lines holds one data set. A line contains the data set number K, a single space, the number of terms to compute M (1≤M≤60), a single space, and the starting value of the RATS sequence. The starting value counts as one term. It is a decimal integer whose digits never decrease from left to right, with at most 40 digits. Later terms may be longer.
Print one line per data set. Give the first term index 1 and inspect the terms in order up to term M.
If one of the first M terms belongs to the creeper, print the data set number, a space, the upper case letter C, a space, and the index of the first term that belongs to the creeper.
Otherwise, if one of the first M terms has the same value as an earlier term of the same sequence, print the data set number, a space, the upper case letter R, a space, and the index of that first repeated term.
Otherwise, print the data set number, a space, and the Mth term.