1561:循环数

文件提交:无需freopen 内存限制:128 MB 时间限制:1.000 S
评测方式:普通裁判 命题人:
提交:8 解决:4

题目描述

循环数是那些不包括 lns="http://www.w3.org/1998/Math/MathML">0 且没有重复数字的整数(比如 lns="http://www.w3.org/1998/Math/MathML">81362)并且还应同时具有一个有趣的性质, 就像这个例子:

如果你从最左边的数字开始(在这个例子中 lns="http://www.w3.org/1998/Math/MathML">8)向右数最左边这个数(如果数到了最右边就回到最左边),你会停止在另一个新的数字(如果停在一个相同的数字上,这个数就不是循环数)。

就像:lns="http://www.w3.org/1998/Math/MathML">8\ 1\ 3\ 6\ 2 从最左边接下去数 lns="http://www.w3.org/1998/Math/MathML">8 个数字: lns="http://www.w3.org/1998/Math/MathML">1\ 3\ 6\ 2\ 8\ 1\ 3\ 6 所以下一个数字是 lns="http://www.w3.org/1998/Math/MathML">6

重复这样做 (这次从 6 开始数 lns="http://www.w3.org/1998/Math/MathML">6 个数字) 并且你会停止在一个新的数字上: lns="http://www.w3.org/1998/Math/MathML">2\ 8\ 1\ 3\ 6\ 2, 也就是 lns="http://www.w3.org/1998/Math/MathML">2

再这样做 (这次数两个): lns="http://www.w3.org/1998/Math/MathML">8\ 1

再一次 (这次一个): lns="http://www.w3.org/1998/Math/MathML">3

又一次: lns="http://www.w3.org/1998/Math/MathML">6\ 2\ 8

这时你回到了起点,在经过每个数字一次后回到起点的就是循环数。如果你经过每一个数字一次以后没有回到起点, 你的数字不是一个循环数。

给你一个数字 lns="http://www.w3.org/1998/Math/MathML">m ,找出第一个比 lns="http://www.w3.org/1998/Math/MathML">m 大的循环数, 输出数据保证结果能用一个无符号长整型数 lns="http://www.w3.org/1998/Math/MathML">\in [0,2^{32}) 装下。 (追加提醒:循环数每个数位都必须要访问到)

输入

仅仅一行, 包括 $m$。

输出

仅仅一行,输出第一个比 $m$ 大的循环数。

样例输入-1 复制

81361

样例输出-1 复制

81362