Pagini recente » Borderou de evaluare (job #1900523) | Cod sursa (job #230203) | Cod sursa (job #2342172) | Cod sursa (job #2980987) | Cod sursa (job #3233333)
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>
using namespace std;
int main() {
ifstream infile("oite.in");
ofstream outfile("oite.out");
int C;
long long L;
infile >> C >> L;
vector<long long> A(C);
for (int i = 0; i < C; ++i) {
infile >> A[i];
}
int count = 0;
// Use a hash map to store pairs of sums
unordered_map<long long, int> pairs_sum;
// Iterate over all pairs of elements
for (int i = 0; i < C; ++i) {
for (int j = i + 1; j < C; ++j) {
long long current_sum = A[i] + A[j];
// Check if there exists another pair that together with the current pair sums to L
if (pairs_sum.find(L - current_sum) != pairs_sum.end()) {
count += pairs_sum[L - current_sum];
}
}
// After processing pairs with the i-th element, store their sums in the hash map
for (int k = 0; k < i; ++k) {
pairs_sum[A[i] + A[k]]++;
}
}
outfile << count << endl;
infile.close();
outfile.close();
return 0;
}