#P1467. [USACO2.2] 循环数 Runaround Numbers
[USACO2.2] 循环数 Runaround Numbers
Description
Runaround numbers are positive integers that contain no and have no repeated digits (for example, ). They must also have the following property:
Starting from the leftmost digit (in , that digit is ), move to the right a number of positions equal to the value of the current digit (wrapping around to the leftmost digit after the rightmost one; in , you move steps). You will land on another new digit (in , this is $\color{red}8\color{black}\to 1\to 3\to 6\to 2\to 8\to 1\to 3\to \color{red}6$; if you land on a digit that has already been visited, then the number is not a runaround number).
Repeat this process. If you return to the leftmost digit after visiting every digit exactly once, then the integer is a runaround number.
Using as an example, the following simulation shows that is a runaround number:
$$\color{red}8\color{black}\to 1\to 3\to 6\to 2\to 8\to 1\to 3\to \color{red}6$$$$\color{red}6\color{black}\to 2\to 8\to 1\to 3\to 6\to \color{red}2$$ $$\color{red}3\color{black}\to 6\to 2\to \color{red}8$$Task: Given a positive integer , find the smallest runaround number that is greater than .
It is guaranteed that .
Input Format
A single line containing .
Output Format
A single line containing the smallest runaround number such that .
81361
81362
Hint
Constraints: For of the testdata, .
Source: USACO 2.2.
Translated by ChatGPT 5
京公网安备 11011102002149号