Cod sursa(job #33195)

Utilizator gcosminGheorghe Cosmin gcosmin Data 18 martie 2007 23:22:11
Problema Oite Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define NMAX 1030
#define NRB 17
#define JEG 3

int NR;

int N, L;

int a[NMAX];

int hash[1 << (NRB + 1)][JEG];
int nsum[1 << (NRB + 1)][JEG];
int nr[1 << (NRB + 1)];

inline void insert_hash( int sum)
{
	int i;
	int hs = ((unsigned int) sum * NR) >> (32 - NRB);

	for (i = 0; i < nr[hs]; i++) if (hash[hs][i] == sum) break;

	if (i < nr[hs]) {
		nsum[hs][i]++;
		return;
	}

	hash[hs][nr[hs]] = sum;
	nsum[hs][nr[hs]] = 1;
	nr[hs]++;
}

inline int srch(int sum)
{
	int hs = ((unsigned int) sum * NR) >> (32 - NRB);

	int i;
	for (i = 0; i < nr[hs]; i++) if (hash[hs][i] == sum) return nsum[hs][i];
	
	return 0;
}

inline int get_hash(int x) { return ((unsigned int) x * NR) >> (32 - NRB); }

int main()
{
	NR = 123151237;
	
	int i, j;
	
	freopen("oite.in", "r", stdin);
	freopen("oite.out", "w", stdout);

	scanf("%d %d", &N, &L);

	for (i = 1; i <= N; i++) scanf("%d", &a[i]);

	long long rez = 0;

	for (i = 1; i <= N; i++) {
		for (j = i + 1; j <= N; j++) rez += srch(L - a[i] - a[j]);
		for (j = 1; j < i; j++) insert_hash(a[i] + a[j]);
	}

	printf("%lld\n", rez);

fclose(stdin);
fclose(stdout);
return 0;
}