UVa 10812 - Beat the Spread!

10812 - Beat the Spread!

 * http://acm.uva.es/p/v108/10812.html

Summary
This is a pretty standard formula manipulation math problem.

Explanation
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 $$a \geq b$$. We get

$$a + b = s$$ and $$a - b = d$$.

Solving for a in terms of b, we get: $$a = s - b$$

plugging it into the latter equation:

$$s - b - b = d$$

or

$$s - d = 2b$$

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 $$( s + d )$$ and $$( s - d )$$ are both divisible by 2. :-)

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

Output
30 10 impossible impossible 53 47 impossible impossible impossible 32 31