TopcoderArchive
SRM17 Sep 2019
SRM 767

ArpaKonkoori

Problem statement, definition, constraints, and public examples.

Problem Statement

Arpa took part in an exam called Konkoor. Here is one of questions that was on the exam:

You are given positive integers d and s. Find nonnegative integers a and b such that gcd(a, b) = d and a^2 - b^2 = s.

Given the longs d and s, compute and return the largest possible value of a. If there is no solution, return -1 instead.

Definition

Class:
ArpaKonkoori
Method:
getA
Parameters:
long, long
Returns:
long
Method signature:
long getA(long d, long s)
(be sure your method is public)

Constraints

  • d will be between 1 and 10^12, inclusive.
  • s will be between 1 and 10^12, inclusive.

Examples

  1. 5
    2975
    Returns: 300
    There are two valid pairs (a, b): (300, 295) and (60, 25). Thus, the largest possible value of a is 300.
  2. 1
    2975
    Returns: 1488
    This time there are four solutions. The one with the largest value a is a=1488, b=1487.
  3. 1
    100
    Returns: -1
    There is no valid solution. There are some pairs (a, b) such that a^2 - b^2 = 100. However, none of them have gcd(a, b) = 1.
  4. 12345678901
    345
    Returns: -1
  5. 3
    9
    Returns: 3
    The only solution here is a=3, b=0.
Back to all problems