#P1467. [USACO2.2] Runaround Numbers

[USACO2.2] Runaround Numbers

题目描述

Runaround numbers are integers with unique digits, none of which is zero (e.g., 8136281362) that also have an interesting property, exemplified by this demonstration:

  • If you start at the left digit (88 in our number) and count that number of digits to the right, wrapping back to the first digit when no digits on the right are available, you will end up at a new digit. A number which does not end up at a new digit is not a runaround number. Consider: 8 1 3 6 28\ 1\ 3\ 6\ 2 which cycles through eight digits: 1 3 6 2 8 1 3 61\ 3\ 6\ 2\ 8\ 1\ 3\ 6 so the next digit is 66.

  • Repeat this cycle, this time for the six counts designed by the 66, and you should end on a new digit: 2 8 1 3 6 22\ 8\ 1\ 3\ 6\ 2, namely 22.

  • Repeat again, two digits this time: 8 18\ 1.

  • Continue again, one digit this time: 33.

  • One more time: 6 2 86\ 2\ 8 and you have ended up back where you started after touching each digit once. If you do not end up back where you started after touching each digit once, your number is not a runaround number.

Given a number MM that has anywhere from 11 through 99 digits, find and print the next runaround number higher than MM. It will always fit into an unsigned long integer for the given test data.

输入格式

A single line with a single integer MM.

输出格式

A single line containing the next runaround number higher than the input value MM.

81361

81362

提示

USACO Training Section 2.2.