Connection Status:
Competition Arena > SumAndProductPuzzle
TCO14 Round 2B · 2014-03-26 · by misof · Math, Simple Search, Iteration
Class Name: SumAndProductPuzzle
Return Type: long
Method Name: getSum
Arg Types: (int, int)
Problem Statement

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.

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
0)
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).

1)
8
11
Returns: 19
2)
40
43
Returns: 0

Sometimes the given range doesn't contain any valid S. In such case the correct return value is 0.

3)
47
74
Returns: 168
4)
4980000
5000000
Returns: 2874227618

Submissions are judged against all 106 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class SumAndProductPuzzle with a public method long long getSum(int A, int B) · 106 test cases · 2 s / 256 MB per case

Submitting as anonymous