Cod sursa(job #226253)

Utilizator ProtomanAndrei Purice Protoman Data 1 decembrie 2008 12:44:52
Problema Oite Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.16 kb
#include <stdio.h>
#include <algorithm>
#include <math.h>
#define mx 4097
#define bz_hash 851731

using namespace std;

struct poz
{
	int s, ct;
	poz *ua;
} *hash[bz_hash + 4], *hash_num[mx];
int n, l, nr;
int a[1024];
long long rez;
double A;

void cohn(long loc, int sum)
{
	for (poz *p = hash_num[loc]; p; p = p->ua)
		if (p->s == sum)
		{
			p->ct++;
			return;
		}
	poz *p = new poz;
	p->s = sum;
	p->ct = 1;
	p->ua = hash_num[loc];
	hash_num[loc] = p;
}


void coh(long loc, int sum)
{
	for (poz *p = hash[loc]; p; p = p->ua)
		if (p->s == sum)
		{
			p->ct++;
			return;
		}
	poz *p = new poz;
	p->s = sum;
	p->ct = 1;
	p->ua = hash[loc];
	hash[loc] = p;
}

int ver_num(long loc, int sum)
{
	for (poz *p = hash_num[loc]; p; p = p->ua)
		if (p->s == sum)
			return p->ct;
	return 0;
}

int ver(long loc, int sum)
{
	for (poz *p = hash[loc]; p; p = p->ua)
		if (p->s == sum)
			return p->ct;
	return 0;
}

int main()
{
	double A = (sqrt((double) 5) - 1) / 2;
	freopen("oite.in","r",stdin);
	freopen("oite.out","w",stdout);
	scanf("%d %d", &n, &l);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		double gs = a[i] * A - (int) (a[i] * A);
		int loc = (double) gs * mx;
		cohn(loc, a[i]);
	}
	for (int i = 1; i < n; i++)
		for (int j = i + 1; j <= n; j++)
		{
			int sp = a[i] + a[j];
			double gs = sp * A - (int) (sp * A);
			int loc = (double) gs * bz_hash;
			coh(loc, sp);
		}
	for (int i = 1; i < n; i++)
		for (int j = i + 1; j <= n; j++)
			if (l > a[i] + a[j])
			{
				int sp = l - (a[i] + a[j]);
				double gs = sp * A - (int) (sp * A);
				int loc = (double) gs * bz_hash;
				int xp = ver(loc, sp);

				int e1 = sp - a[i];
				gs = e1 * A - (int) (e1 * A);
				loc = (double) gs * mx;
				int nrp = ver_num(loc, e1);

				if (e1 == a[i])
					nrp--;
				xp -= nrp;

				int e2 = sp - a[j];
				gs = e2 * A - (int) (e2 * A);
				loc = (double) gs * mx;
				nrp = ver_num(loc, e2);

				if (e2 == a[j])
					nrp--;
				xp -= nrp;

				if (sp == a[i] + a[j])
					xp++;
				rez += xp;
			}
	printf("%lld\n", rez / 6);
	fclose(stdin);
	fclose(stdout);
	return 0;
}