UVa 10407 - Simple division

10407 - Simple Division

 * http://acm.uva.es/p/v104/10407.html

Summary
An interesting problem in Number Theory.

Explanation
Let the list of positive integers be $$ x_1, x_2, ..., x_n, n \ge 2 $$. We can rewrite the list as $$ x_1, x_1 + d_1, x_2 + d_2, ..., x_{n-1} + d_{n-1}, n \ge 2 $$, where $$d_i$$ is the difference between $$x_{i+1}$$ and $$x_i$$. Let m be a positive integer such that $$ x_1 \equiv x_2 \equiv ... \equiv x_n$$ mod m.

Then the first useful fact is that m can not be bigger than any $$d_i$$ i=1..n-1. Otherwise, suppose that m > $$d_i$$ for some i. Let $$ x_i \equiv r $$ mod m, i.e. $$x_i$$ = km + r, r<m. Then $$ x_{i+1} = x_i + d_i = km + r + d_i $$. if $$ r + d_i < m $$, then $$ x_{i+1} \equiv r + d_i $$ mod m. if $$ r + d_i \ge m $$, then $$ x_{i+1} \equiv r + d_i - m$$ mod m. Either way, $$ x_{i+1} \not\equiv x_i$$ mod m.

Another important fact about m is that m has to divide all $$d_i$$'s, i=2..n. Because $$ x_{i+1} = x_i + d_i \equiv x_i $$ mod m if and only if $$ d_i \equiv 0$$ mod m. Combining the two facts, we know that the solution of the problem is the greatest common divisor of the $$d_i$$'s, i=2..n.

Gotchas

 * There may be duplicate integers in the input.

Input
701 1059 1417 2312 0 701 1059 1059 1417 2312 0 14 23 17 32 122 0 14 -22 17 -31 -124 0 0 179 179 3 3