-
Notifications
You must be signed in to change notification settings - Fork 0
/
1101D. GCD Counting.cpp
138 lines (116 loc) · 3.16 KB
/
1101D. GCD Counting.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <deque>
#include <stack>
#include <numeric>
#include <memory>
#include <list>
#include <climits>
#include <fstream>
#include <sstream>
#include <random>
#include <functional>
#define PB push_back
#define F first
#define S second
#define REP(i,from,to) for(auto i=(from); i<=(to); ++i)
#define PER(i,from,to) for(auto i=(from); i>=(to); --i)
#define REP_IF(i,from,to,assert) for(auto i=(from); i<=(to); ++i) if (assert)
#define FOR(i,less_than) for (auto i=0; i<(less_than); ++i)
#define FORI(i, container) for (auto i=0; i<(container).size(); ++i)
#define FORI_IF(i, container, assert) for (auto i=0; i<(container).size(); ++i) if (assert)
#define ROFI(i, container) for (auto i=SZ(container)-1; i>=0; --i)
#define FOREACH(elem, container) for (auto elem : (container))
#define MEMSET(container, value) memset(container, value, sizeof(container))
#define FILL(container, value) fill(container.begin(), container.end(), value)
#define FILL0(container) fill(container.begin(), container.end(), 0)
#define ALL(container) (container).begin(), (container).end()
#define SZ(container) (int)((container).size())
#define BACK(set_map) *prev((set_map).end(), 1)
#define FRONT(set_map) *(set_map).begin()
using PII = std::pair<int,int>;
using LL = long long;
using VI = std::vector<int>;
using VLL = std::vector<LL>;
using VVI = std::vector<VI>;
using VVLL = std::vector<VLL>;
using namespace std;
const int MAXN = 200007;
VI primes;
int n, ans = 0;
vector<PII> f[MAXN];
VI e[MAXN];
VI a;
void init() {
VI vis(MAXN+1);
REP(i, 2, MAXN) {
if (!vis[i]) primes.PB(i);
for (int j=0; j<primes.size() && primes[j]*i<=MAXN; ++j) {
vis[primes[j]*i] = true;
if (i%primes[j] == 0) break;
}
}
cin >> n;
a.resize(n + 1);
REP(i, 1, n) cin >> a[i];
for (int u,v,i=0; i+1<n; ++i) {
cin >> u >> v;
e[u].PB(v), e[v].PB(u);
}
}
VI factorization(int x) {
VI ans;
for (int i=0; x>1 && x>=primes[i]*primes[i]; ++i)
if (x % primes[i] == 0) {
ans.PB(primes[i]);
while (x%primes[i] == 0)
x /= primes[i];
}
if (x > 1) ans.PB(x);
return ans;
}
void dfs(int x, int pre) {
vector<PII> sons;
FOREACH(y, e[x])
if (y != pre) {
dfs(y, x);
FOREACH(pr, f[y]) sons.PB(move(pr));
}
sort(ALL(sons));
for (int j,i=0; i<SZ(sons); i=j) {
int mx1=0, mx2=0;
for (j=i; j < SZ(sons) && sons[j].F==sons[i].F; ++j)
if (sons[j].S > mx1)
mx2 = mx1, mx1 = sons[j].S;
else if (sons[j].S > mx2)
mx2 = sons[j].S;
if (a[x]%sons[i].F == 0) {
ans = max(ans, mx1+mx2+1);
f[x].PB({sons[i].F, max(mx1, mx2)+1});
while (a[x]%sons[i].F == 0) a[x] /= sons[i].F;
}
}
auto factors = factorization(a[x]);
FOREACH(factor, factors) {
f[x].emplace_back(factor, 1);
ans = max(ans, 1);
}
}
int main(int argc, char const *argv[]) {
ios::sync_with_stdio(false);
init();
dfs(1, -1);
cout << ans << endl;
return 0;
}