Cod sursa(job #476229)

Utilizator darrenRares Buhai darren Data 10 august 2010 12:25:56
Problema Oite Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <fstream>
#include <algorithm>
#include <list>

using namespace std;

const int MOD = 3033;
list<pair<int, int> > hash[MOD];

void add(int value)
{
	int nMOD = value % MOD;
	for (list<pair<int ,int> >::iterator it = hash[nMOD].begin(); it != hash[nMOD].end(); ++it)
		if ((*it).first == value)
		{
			++(*it).second;
			return;
		}
	hash[nMOD].push_back(make_pair(value, 1));
}

void del(int value)
{
	int nMOD = value % MOD;
	for (list<pair<int ,int> >::iterator it = hash[nMOD].begin(); it != hash[nMOD].end(); ++it)
		if ((*it).first == value)
		{
			--(*it).second;
			return;
		}
}

int find(int value)
{
	int nMOD = value % MOD;
	for (list<pair<int ,int> >::iterator it = hash[nMOD].begin(); it != hash[nMOD].end(); ++it)
		if ((*it).first == value)
			return (*it).second;
	return 0;
}

int n, r, a[1028];
long long nsol;
int main()
{
	ifstream fin("oite.in");
	ofstream fout("oite.out");
	fin >> n >> r;
	for (int i = 0; i < n; ++i)
		fin >> a[i];

	sort(a, a + n);

	for (int i = 2; i < n - 1; ++i)
		for (int j = i + 1; j < n; ++j)
			add(a[i] + a[j]);

	for (int i = 1; i < n - 1; ++i)
	{
		for (int j = 0; j < i; ++j)
			nsol += find(r - a[i] - a[j]);
		for (int j = i + 2; j < n; ++j)
			del(a[i + 1] + a[j]);
	}

	fout << nsol;
}