What the Flex?

시간 제한0.5초메모리 제한1024 MB

요약
a와 N이 주어질 때, a와 소인수 집합이 같은 [1,N] 범위의 수들을 지수 튜플의 사전순으로 나열했을 때 a의 다음 수를 구한다.
난이도

보통10점 중 7점

유형
정수론, 수학, 이분 탐색, 그리디
정답자
아직 제출이 없습니다

문제

\textit{In this problem, you have to find the next integer in a particular ordering with has the same set of prime divisors.}

Archaeologist Kris studies the ancient platform Flex which was recently found near Adobe creek. On this platform, the numbers of different versions of teleporters which have been created by The Elders were once inscribed. Each version number is an integer between 11 and NN. On the platform, there are several columns of numbers, one for each teleporter. Each column once listed all created versions of a teleporter in the order they were built. Unfortunately, some of the numbers were erased.

Kris just finished studying version aa of teleporter bb, and he is now ready to examine the next version of this teleporter. But he does not even know its number! On the other hand, any program which is a correct solution of this problem must know it. Below is what Kris already knows about the version numbers of the teleporters:

  1. Each number from 11 to NN is the number of some version of some teleporter.
  2. Different versions of one teleporter have different numbers.
  3. Each teleporter has its own set of prime numbers. Later, we consider one fixed teleporter. Let its prime number set be X=p_1,p_2,…,p_mX = \\{p\_1, p\_2, \dots, p\_m\\} (we list them so that p_1<p_2<⋯<p_mp\_1 < p\_2 < \dots < p\_m).
  4. It is known that any version number of this teleporter is divisible by each prime from XX and not divisible by primes not in X. It means that such number can be written as p_1k_1p_2k_2…p_mk_mp\_1^{k\_1} p\_2^{k\_2} \dots p\_m^{k\_m} where k_i≥1k\_i \ge 1. So, each version of this teleporter can be mapped to a tuple of mm positive integers (k_1,k_2,…,k_m)(k\_1, k\_2, \dots, k\_m).
  5. Version α=(k_1,k_2,…,k_m)\alpha = (k\_1, k\_2, \dots, k\_m) was built before version β=(l_1,l_2,…,l_m)\beta = (l\_1, l\_2, \dots, l\_m) if and only if there is such ii (0≤i<m0 \le i < m) that k_1=l_1k\_1 = l\_1, k_2=l_2k\_2 = l\_2, …\dots, k_i=l_ik\_i = l\_i, but k_i+1<l_i+1k\_{i+1} < l\_{i+1}. It can be said that the versions are ordered lexicographically by their tuples.

입력

The only line of input contains two integers --- the version number aa of a teleporter and the number NN (1≤a≤N≤10181 \le a \le N \le 10^{18}).

출력

If there exists a version of the same teleporter which was built just after version aa, print its number. Otherwise, print "-1".

예제2

  1. 예제 1

    입력
    6 13
    
    예상 출력
    12
    
  2. 예제 2

    입력
    12 13
    
    예상 출력
    -1