What the Flex?
Time limit0.5sMemory limit1024 MB
Given version a and limit N, find the next version after a that has the same set of prime divisors, or print -1 if none exists.
- Level
Medium6 of 10
- Topics
- Number theory, Greedy, Math
- Solved
- No attempts yet
Problem
\textit{In this problem, you have to find the next integer in a particular ordering that 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 version numbers of teleporters built by The Elders were once inscribed. Each version number is an integer between and . There is one column of numbers for each teleporter. Each column lists all created versions of that teleporter in the order they were built. Some of the numbers were erased.
Kris just finished studying version of teleporter , and he is now ready to examine the next version of this teleporter. He does not know its number, but any correct solution must find it. Here is what Kris already knows:
- Each number from to is the version number of some version of some teleporter.
- Different versions of one teleporter have different numbers.
- Each teleporter has its own set of prime numbers. Consider one fixed teleporter with prime set , where .
- Every version number of this teleporter is divisible by each prime in and by no prime outside . Such a number can be written as with . Each version maps to a tuple of positive integers.
- Version was built before version if and only if there is an () with and . In other words, the versions are ordered lexicographically by their tuples.
Input
The only line of input contains two integers, the version number of a teleporter and the number ().
Output
If there is a version of the same teleporter that was built just after version , print its number. Otherwise, print "-1".