forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1535B - Array Reodering.cpp
80 lines (57 loc) · 1.58 KB
/
1535B - Array Reodering.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
/*
You are given an array a consisting of n integers.
Let's call a pair of indices i, j good if 1=i<j=n and gcd(ai,2aj)>1 (where gcd(x,y) is the greatest common divisor of x and y).
Find the maximum number of good index pairs if you can reorder the array a in an arbitrary way.
Input
The first line contains a single integer t (1=t=1000) — the number of test cases.
The first line of the test case contains a single integer n (2=n=2000) — the number of elements in the array.
The second line of the test case contains n integers a1,a2,…,an (1=ai=105).
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output a single integer — the maximum number of good index pairs if you can reorder the array a in an arbitrary way.
Example
inputCopy
3
4
3 6 5 3
2
1 7
5
1 4 2 4 1
outputCopy
4
0
9
Note
In the first example, the array elements can be rearranged as follows: [6,3,5,3].
In the third example, the array elements can be rearranged as follows: [4,4,2,1,1].
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector <int> v;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (a % 2) v.push_back(a);
}
int even = n - v.size();
int odd = v.size();
long long cnt = n - 1, res = 0;
res += (even * (2 * (n - 1) - (even - 1))) / 2;
for (int i = 0; i < odd; i++) {
for (int j = i + 1; j < odd; j++) {
if (__gcd(v[i], v[j]) != 1) res++;
}
}
cout << res << "\n";
}
return 0;
}