-
Notifications
You must be signed in to change notification settings - Fork 185
/
1081.cc
60 lines (55 loc) · 1005 Bytes
/
1081.cc
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
// https://cses.fi/problemset/task/1081/
#include <iostream>
#include <vector>
#include <set>
using namespace std;
typedef vector<int> vi;
typedef set<int, greater<int>> si;
const int X = 1000000;
int a[X+1];
bool d[X+1];
int m;
void divisors(vi& f, si& s, int i, int x) {
if (i == f.size()) {
s.insert(x);
return;
}
divisors(f, s, i + 1, x);
divisors(f, s, i + 1, x * f[i]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
for (int i = 2; i <= X; i++) {
if (a[i]) continue;
for (int j = 2*i; j <= X; j += i)
a[j] = i;
}
int n;
cin >> n;
while (n--) {
int x; cin >> x;
if (x < m) continue;
if (d[x]) {
m = max(m, x);
continue;
}
vi f;
while (a[x]) {
f.push_back(a[x]);
x /= a[x];
}
f.push_back(x);
si s;
divisors(f, s, 0, 1);
for (auto c:s) {
if (c <= m) break;
if (d[c]) {
m = max(m, c);
break;
}
d[c] = true;
}
}
cout << m << endl;
}