TopcoderArchiveVisit Topcoder
TCO26 Mar 2014
TCO14 Parallel Round 2B

SumAndProductPuzzle

Problem statement, definition, constraints, and public examples.

Problem Statement

Consider the following story.

STORY STARTS HERE.

There were three mathematicians: Susan, Priscilla, and Bob. Bob picked two positive integers x and y such that x <= y. He then told their sum to Susan and their product to Priscilla. Susan and Priscilla both knew all the facts listed above. Then, Susan and Priscilla made the following statements:

  • Susan: "I am certain that you cannot determine my number."
  • Priscilla: "Thanks for telling me that. Now I'm sure that your number is S."

STORY ENDS HERE.

My friends Baska and Olivia are fond of puzzles. Recently, Baska told Olivia the above story. When telling the story, Baska used some specific positive integer (for example, 9) instead of S. Then, she asked Olivia to determine x and y. Olivia easily came up with the unique solution.

Of course, you don't know the integer Baska used instead of S. Instead, you are given two ints A and B. Find all S between A and B, inclusive, such that the above discussion between Baska and Olivia could have happened. Return the sum of all such S.

Definition

Class:
SumAndProductPuzzle
Method:
getSum
Parameters:
int, int
Returns:
long
Method signature:
long getSum(int A, int B)
(be sure your method is public)

Notes

  • Watch out for overflow. The return value may overflow a 32-bit integer variable.

Constraints

  • A will be between 1 and 5,000,000, inclusive.
  • B will be between A and 5,000,000, inclusive.

Examples

  1. 30
    33
    Returns: 33
    The only valid S in this range is 33. The unique pair (x,y) that corresponds to S=33 is (1,32).
  2. 8
    11
    Returns: 19
  3. 40
    43
    Returns: 0
    Sometimes the given range doesn't contain any valid S. In such case the correct return value is 0.
  4. 47
    74
    Returns: 168
  5. 1
    2
    Returns: 0
Back to all problems