SumAndProductPuzzle
TCO14 Round 2B · 2014-03-26 · by misof
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
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.
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).
8 11 Returns: 19
40 43 Returns: 0
Sometimes the given range doesn't contain any valid S. In such case the correct return value is 0.
47 74 Returns: 168
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.
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