-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy path1742D-Coprime.cpp
37 lines (30 loc) · 943 Bytes
/
1742D-Coprime.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
#include <cstdio>
#include <vector>
long gcd (long a, long b){return (b == 0) ? a : gcd (b, a%b);}
int main(){
long B = 1001;
std::vector<std::vector<long> > m(B);
for(long p = 1; p < B; p++){
for(long q = 1; q < B; q++){
if(gcd(p, q) > 1){continue;}
m[p].push_back(q); m[q].push_back(p);
}
}
long t; scanf("%ld", &t);
while(t--){
long n; scanf("%ld", &n);
std::vector<long> where(B, -1);
for(long p = 0; p < n; p++){long x; scanf("%ld", &x); where[x] = p + 1;}
long res(-1);
for(long p = 1; p < B; p++){
if(where[p] < 0){continue;}
for(long u = 0; u < m[p].size(); u++){
long q = m[p][u];
if(where[q] < 0){continue;}
long sum = where[p] + where[q];
res = (res > sum ? res : sum);
}
}
printf("%ld\n", res);
}
}