CF303D.Rotatable Number

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Bike is a smart boy who loves math very much. He invented a number called "Rotatable Number" inspired by 142857.

As you can see, 142857 is a magic number because any of its rotatings can be got by multiplying that number by 1, 2, ..., 6 (numbers from one to number's length). Rotating a number means putting its last several digit into first. For example, by rotating number 12345 you can obtain any numbers: 12345, 51234, 45123, 34512, 23451. It's worth mentioning that leading-zeroes are allowed. So both 4500123 and 0123450 can be obtained by rotating 0012345. You can see why 142857 satisfies the condition. All of the 6 equations are under base 10.

  • 142857·1 = 142857;
  • 142857·2 = 285714;
  • 142857·3 = 428571;
  • 142857·4 = 571428;
  • 142857·5 = 714285;
  • 142857·6 = 857142.

Now, Bike has a problem. He extends "Rotatable Number" under any base b. As is mentioned above, 142857 is a "Rotatable Number" under base 10. Another example is 0011 under base 2. All of the 4 equations are under base 2.

  • 0011·1 = 0011;
  • 0011·10 = 0110;
  • 0011·11 = 1001;
  • 0011·100 = 1100.

So, he wants to find the largest b (1 < b < x) so that there is a positive "Rotatable Number" (leading-zeroes allowed) of length n under base b.

Note that any time you multiply a rotatable number by numbers from 1 to its length you should get a rotating of that number.

Bike 是一个聪明的男孩,非常热爱数学。他受数字 142857 的启发,发明了一类被称为“可旋转数”(Rotatable Number)的数。

如你所见,142857 是一个神奇的数,因为它的任意一种旋转形式均可通过将该数分别乘以 1,2,…,61, 2, \dots, 6(即从 11 到该数的位数)得到。“旋转一个数”是指将该数末尾若干位数字移到最前面。例如,对数字 1234512345 进行旋转,可得到如下所有数字:12345, 51234, 45123, 34512, 2345112345,\ 51234,\ 45123,\ 34512,\ 23451。需要特别说明的是,允许前导零。因此,45001234500123 和 01234500123450 均可通过旋转 00123450012345 得到。你可以由此理解为何 142857142857 满足该条件。以下全部等式均在十进制(base 10)下成立:

  • 142857⋅1=142857142857 \cdot 1 = 142857;
  • 142857⋅2=285714142857 \cdot 2 = 285714;
  • 142857⋅3=428571142857 \cdot 3 = 428571;
  • 142857⋅4=571428142857 \cdot 4 = 571428;
  • 142857⋅5=714285142857 \cdot 5 = 714285;
  • 142857⋅6=857142142857 \cdot 6 = 857142。

现在,Bike 遇到了一个问题:他将“可旋转数”的定义推广到了任意进制 bb。如前所述,142857142857 是一个十进制下的“可旋转数”。另一个例子是二进制(base 2)下的 00110011。以下全部等式均在二进制下成立:

  • 0011⋅1=00110011 \cdot 1 = 0011;
  • 0011⋅10=01100011 \cdot 10 = 0110;
  • 0011⋅11=10010011 \cdot 11 = 1001;
  • 0011⋅100=11000011 \cdot 100 = 1100。

于是,他希望找出满足以下条件的最大进制 bb(其中 1<b<x1 < b < x):存在一个长度为 nn 的正整数“可旋转数”(允许前导零),且该数在进制 bb 下是可旋转的。

注意:每次将该可旋转数乘以从 11 到其位数(即 nn)之间的任意整数时,所得结果必须恰好是该数的某一种旋转形式。

输入格式

The only line contains two space-separated integers n, x (1 ≤ n ≤ 5·106, 2 ≤ x ≤ 109).

唯一一行包含两个以空格分隔的整数 nn 和 xx(1 ≤ n ≤ 5⋅1061 \leq n \leq 5\cdot10^6,2 ≤ x ≤ 1092 \leq x \leq 10^9)。

输出格式

Print a single integer — the largest b you found. If no such b exists, print -1 instead.

输出一个整数——即你找到的最大的 b。如果不存在这样的 b,则输出 -1。

输入输出样例

  • 输入#1

    6 11

    输出#1

    10
  • 输入#2

    5 8

    输出#2

    -1

输入解题思路,AI测评打分。不知道怎么写?

首页