AcWing 3543. 三元组
原题链接
简单
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1e2 + 10;
int t, n, m, k, l, r, op, x, y, ans;
int f[N];
unordered_map<int, int>mp;
void solve() {
cin >> n;
while (n--) {
ans = 0;
mp.clear();
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> f[i];
mp[f[i]]++;
}
sort(f + 1, f + 1 + m);
m = unique(f + 1, f + 1 + m) - f - 1;
for (int i = 1; i <= m; i++) {
for (int j = i; j <= m; j++) {
if (mp.count(f[i] + f[j])) {
if (i == j) {
ans += mp[f[i]] * mp[f[i]] * mp[f[i] + f[j]];
} else {
ans += mp[f[i]] * mp[f[j]] * mp[f[i] + f[j]] * 2;
}
}
}
}
cout << ans << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}