TopcoderArchive
SRM31 Oct 2019
SRM 770

MaximumMoves

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given two nonnegative integers P and Q. Your task is to make the integers equal in as many steps as possible. In each step you can do one of two things:

  • Add any prime number to P.
  • Subtract any prime number from Q.

If it's not possible to make P and Q equal, return -1. Otherwise, return a nonnegative integer: the maximum (not minimum!) number of steps you can make.

Definition

Class:
MaximumMoves
Method:
getMaximumMoves
Parameters:
long, long
Returns:
long
Method signature:
long getMaximumMoves(long P, long Q)
(be sure your method is public)

Notes

  • Primes are positive integers with exactly two positive integer divisors. The smallest few primes are 2, 3, 5, 7, 11, 13, ...
  • In particular, the numbers 0 and 1 are not prime numbers.

Constraints

  • P will be between 0 and 10^18, inclusive.
  • Q will be between 0 and 10^18, inclusive.

Examples

  1. 5
    9
    Returns: 2
    In one optimal solution we first subtract 2 from Q and then add 2 to P. After these two steps both numbers are equal (both are 7).
  2. 5
    10
    Returns: 2
    In one optimal solution we first subtract 2 from Q and then add 3 to P. After these two steps both numbers are equal (both are 8).
  3. 5
    6
    Returns: -1
    There's no way to make these P and Q equal.
  4. 10
    2
    Returns: -1
Back to all problems