forked from ajahuang/UVa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa 10407 - Simple division.cpp
53 lines (43 loc) · 1.17 KB
/
UVa 10407 - Simple division.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
bool comp(const int &a, const int &b)
{
return abs(a) < abs(b);
}
inline int gcd(int a, int b)
{
return b == 0? a : gcd(b, a % b);
}
int main()
{
vector<int> sequence;
int s;
while (cin >> s, s != 0)
{
sequence.clear();
sequence.push_back(s);
while (cin >> s, s != 0)
sequence.push_back(s);
// Sort the sequence by absolute values.
sort(sequence.begin(), sequence.end(), comp);
/**
Rearrange the sequence by their differences.
For example,
sequence = { 2, 5, 11, 26 }
differences = { 3, 6, 15 }
where 3 = 5 - 2, 6 = 11 - 5, 15 = 26 - 11.
*/
for (size_t i = 0; i < sequence.size() - 1; ++i)
sequence[i] = abs(sequence[i + 1] - sequence[i]);
// Erase the last element.
sequence.erase(sequence.end() - 1);
int g = sequence[0];
for (size_t i = 1; i < sequence.size(); ++i)
g = gcd(g, sequence[i]);
cout << g << endl;
}
return 0;
}