-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path1025B. Weakened Common Divisor.cpp
71 lines (56 loc) · 1.08 KB
/
1025B. Weakened Common Divisor.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Idea:
- Brute force the divisors of the first pair, is there is no
answer, then print "No", otherwise print "Yes".
*/
#include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 1;
int n;
pair<int, int> a[N];
vector<int> d;
set<int> st;
void calc_div(int x) {
d.clear();
int sqrtx = sqrt(x) + 1;
for(int i = 1; i <= sqrtx; ++i) {
if(x % i == 0) {
d.push_back(i);
if(x / i != i)
d.push_back(x / i);
}
}
}
void solve() {
for(int i = 0, cur, dd; i < d.size(); ++i) {
if(d[i] == 1)
continue;
cur = 0;
dd = d[i];
if(st.count(dd) == 1)
continue;
st.insert(dd);
for(int j = 1; j < n; ++j)
if(a[j].first % dd == 0 || a[j].second % dd == 0)
++cur;
else
break;
if(cur == n - 1) {
printf("%d\n", dd);
exit(0);
}
}
}
int main() {
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%d %d", &a[i].first, &a[i].second);
sort(a, a + n);
reverse(a, a + n);
calc_div(a[0].first);
solve();
calc_div(a[0].second);
solve();
puts("-1");
return 0;
}