UVa 10407

From Algorithmist

Jump to: navigation, search

Contents

[edit] 10407 - Simple Division

[edit] Summary

An interesting problem in Number Theory.

[edit] 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 di is the difference between xi + 1 and xi. 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 di i=1..n-1. Otherwise, suppose that m > di for some i. Let  x_i \equiv r mod m, i.e. xi = km + r, r<m. Then xi + 1 = xi + di = km + r + di. if r + di < 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 di'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 di's, i=2..n.

[edit] Gotchas

  • There may be duplicate integers in the input.

[edit] Notes

  • In the proof, I am assuming that the integers are positive. But in the problem there may be some negative integers. But it seems that does not hurt the validity of the algorithm. Could anybody prove or disprove it?

[edit] 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
Personal tools