UVa 10778 - Mathemagicland

10778 - Mathemagicland

 * http://acm.uva.es/p/v107/10778.html

Summary
This problem is really asking for all the rational roots of a polynomial with integer coefficients.

Explanation
To see that we have, for n = 3: $$\sigma_1=a+b+c$$ $$\sigma_2=ab+ac+bc$$ $$\sigma_3=abc$$

and that $$a,b,c$$ are the roots of $$x^3-(a+b+c)x^2+(ab+ac+bc)x-abc$$. This holds in general, with the signs alternate between adjacent powers of x, and that the sign of the leading coefficient is 1. Of course, there are a lot of advanced algorithms for factoring, such as Strum's algorithm. But the intent of this problem is just the basics. Recall that you factor stuff in high school using the Rational Roots Theorem and Remainder Theorem. But there is one problem: what happens if there are multiple roots? One idea is to divide the roots out, possibly using Synthetic division. You may also want to avoid using doubles. So how do you achieve synthetic division in this case? We need a simple result from question B1 of the 2004 Putnam Contest. Alternatively, if you know your algebra well enough, Z[x] is a unique factorization domain. Unqiue Factorization is a result of euclidean algorithm, and a root of p(x) is a multiple root if and only if it divides the greatest common divisor of p and its derivative p'.

Input
2 1 2 1 0

Output
1 1