-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriangles.cpp
47 lines (43 loc) · 991 Bytes
/
Triangles.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
#include<bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define debug(a) std::cout << #a " is value " << (a) << std::endl;
#define ull unsigned long long
ull compute(int n, int remainder){
ull sum = 1;
ull pair = (ull) n*(n-1)*remainder / 2;
for(int i = 1; i <= 3; i++){
sum = (ull) sum * (n - 3 + i) / i;
}
return sum + pair;
}
void solve(){
int n;
cin >> n;
ull result = 0;
vector<int> a(n);
map<int, int> m;
int count = 0;
forn (i, n){
cin >> a[i];
m[a[i]]++;
}
for(auto& i: m){
if(i.second == 2) result += count;
else if (i.second > 2){
result += compute(i.second, count);
}
count += i.second;
}
cout << result << " \n";
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t;
cin >> t;
while (t--) solve();
return 0;
}