Simulate a RATS sequence term by term for up to M steps, detecting the first term that repeats an earlier value or first takes the chain form 1233*4444 or 5566*7777.
Medium6SimulationImplementationStringHash mapNo attempts yetTime limit2sMemory limit512 MBA RATS sequence in base 10 starts from one positive integer. Given a term i, add i to the number you get by reversing the digits of i, then sort the digits of that sum into non-decreasing order. The result is the next term. If the sorted digits begin with zeros, drop those zeros.
The term after 12334444 is 55667777, because 12334444 + 44443321 = 56777765 and sorting those digits gives 55667777. The term after 44556 is 111, because 44556 + 65544 = 110100, sorting gives 000111, and dropping the leading zeros leaves 111.
A conjecture says that every RATS sequence enters either a repeat or a chain.
The sequence enters a repeat at the first term that already appeared earlier. The sequence starting with 123 runs 123, 444, 888, 1677, 3489, 12333, 44556, 111, 222, 444, 888, and so on, so it enters a repeat at term 10, where 444 appears for the second time.
The sequence enters a chain at the first term of the form 1233*4444 or 5566*7777. Here 3* means one or more consecutive digits 3, and 6* means one or more consecutive digits 6. After such a term the counts of 3s and 6s keep growing and the terms go to infinity, as in 12334444, 55667777, 123334444, 556667777, 1233334444, 5566667777, and so on.
Write a program that decides whether a RATS sequence enters a repeat or a chain within its first M terms.
The first line contains the number of test cases t (1≤t≤10000).
Each of the next t lines contains two integers: the number of terms to compute, M (1≤M≤60), and the first term of the RATS sequence. The first term is a decimal integer whose digits do not decrease from left to right, and it has at most 40 digits. Later terms may have more than 40 digits.
Print one line for each test case. Terms are numbered from 1, so the first term is term 1.
If the sequence enters a chain within the first M terms, print the uppercase letter C, one space, and the index of the first term that has a chain form.
If the sequence enters no chain but does enter a repeat, print the uppercase letter R, one space, and the index of the first term that already appeared earlier.
If neither happens, print the M-th term.
If both a chain and a repeat occur within the first M terms, print the one with the smaller index.