Cod sursa(job #199973)

Utilizator ProtomanAndrei Purice Protoman Data 21 iulie 2008 15:09:05
Problema Oite Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <stdio.h>
#include <algorithm>
#define mx 1030
#define bz 5355751

using namespace std;

struct poz
{
	long e1, e2;
	poz *ua;
} *hash[bz + 4];
long n, l, rez, nr;
long a[mx];

void coh(long loc, long p1, long p2)
{
	poz *p;
	p = new poz;
	p->e1 = p1;
	p->e2 = p2;
	p->ua = hash[loc];
	hash[loc] = p;
}

void ver(long loc, long p1, long p2)
{
	for (poz *p = hash[loc]; p; p = p->ua)
		if ((p1 != p->e1) && (p2 != p->e1) && (p1 != p->e2) && (p2 != p->e2))
			if (a[p1] + a[p2] + a[p->e1] + a[p->e2] == l)
				rez++;
}

int main()
{
	freopen("oite.in","r",stdin);
	freopen("oite.out","w",stdout);
	scanf("%ld %ld", &n, &l);
	for (int i = 1; i <= n; i++)
		scanf("%ld", &a[i]);
	for (int i = 1; i < n; i++)
		for (int j = i + 1; j <= n; j++)
			coh((a[i] + a[j]) % bz, i, j);
	for (int i = 1; i < n; i++)
		for (int j = i + 1; j <= n; j++)
			if (l > a[i] + a[j])
				ver((l - a[i] - a[j]) % bz, i, j);
	printf("%ld\n", rez / 6);
	fclose(stdin);
	fclose(stdout);
	return 0;
}