-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD_Coprime.cpp
More file actions
50 lines (41 loc) · 1016 Bytes
/
D_Coprime.cpp
File metadata and controls
50 lines (41 loc) · 1016 Bytes
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
// Mohit Verma "mohitvdx"
// problem:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = LLONG_MAX >> 1;
vector<int> pairs[1001]; // this is an array of vectors
void solve() {
int n; cin >> n;
vector<int> id[1001];
for (int i = 1; i <= n; ++i) {
int x; cin >> x;
id[x].push_back(i);
}
int ans = -1;
for (int i = 1; i <= 1000; ++i) {
for (int j : pairs[i]) {
if (!id[i].empty() && !id[j].empty()) {
ans = max(ans, id[i].back() + id[j].back());
}
}
}
cout << ans << "\n";
}
signed main() {
for (int i = 1; i <= 1000; ++i) { //precomputing all co-prime pairs upto 1000
for (int j = 1; j <= 1000; ++j) {
if (gcd(i, j) == 1) {
pairs[i].push_back(j);
}
}
}
ios::sync_with_stdio(false); cin.tie(NULL); // fast IO
int t; cin >> t;
while (t--) {
solve();
}
}
/*
*/