#P1467. [USACO2.2] 循环数 Runaround Numbers

[USACO2.2] 循环数 Runaround Numbers

Description

Runaround numbers are positive integers that contain no 00 and have no repeated digits (for example, 8136281362). They must also have the following property:

Starting from the leftmost digit (in 81362\color{red}{8}\color{black}1362, that digit is 88), 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 81362\color{red}{8}\color{black}1362, you move 88 steps). You will land on another new digit (in 81362\color{red}{8}\color{black}1362, 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 8136281362 as an example, the following simulation shows that 8136281362 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$$281\color{red}2\color{black}\to 8\to \color{red}1 13\color{red}1\color{black}\to \color{red}3 $$\color{red}3\color{black}\to 6\to 2\to \color{red}8$$

Task: Given a positive integer mm, find the smallest runaround number mm' that is greater than mm.

It is guaranteed that m2321m' \le 2^{32} - 1.

Input Format

A single line containing mm.

Output Format

A single line containing the smallest runaround number mm' such that m>mm' > m.

81361

81362

Hint

Constraints: For 100%100\% of the testdata, 1m1091 \le m \le 10^9.

Source: USACO 2.2.

Translated by ChatGPT 5