UVa 10812

From Algorithmist
Jump to navigation Jump to search

10812 - Beat the Spread![edit]

Summary[edit]

This is a pretty standard formula manipulation math problem.

Explanation[edit]

Given the sum of a and b, and the difference of a and b, find a and b. First, make sure the difference is not greater than the sum, since this would imply one of them is negative. Without a lost of generality, assume . We get

and .

Solving for a in terms of b, we get:

plugging it into the latter equation:

or

We then solve for b numerically and plug it into one of the formulas to get a. All that's left is to check that a and b are both non-negative, and we're done.

NOTE: Check that and are both divisible by 2. :-)

Implementations[edit]

Input[edit]

8
40 20
20 40
1 5
100 6
12 61
51 61
123 512
63 1

Output[edit]

30 10
impossible
impossible
53 47
impossible
impossible
impossible
32 31