RATS sequence
Time limit1sMemory limit256 MB
Simulate up to 60 RATS steps per data set and report the first creeper term, the first repeated term, or the last term.
- Level
Medium4 of 10
- Topics
- Simulation, String, Hash map
- Solved
- No attempts yet
Problem
The 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.
- Reverse the digits of the input value.
- Add the reversed value to the input value.
- Sort the digits of the sum into increasing order and drop any leading zeros.
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 copies of the digit 3, then 4444, or when it reads 55, then copies of the digit 6, then 7777, where . 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 terms of a RATS sequence. Also decide whether the sequence repeats a value inside those terms, or enters the creeper inside those terms.
Input
The first line contains the number of data sets (). Each data set is independent and is processed the same way.
Each of the next lines holds one data set. A line contains the data set number , a single space, the number of terms to compute (), 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.
Output
Print one line per data set. Give the first term index 1 and inspect the terms in order up to term .
If one of the first 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 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 th term.