Cod sursa(job #744654)

Utilizator GrimpowRadu Andrei Grimpow Data 9 mai 2012 13:00:55
Problema Oite Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <stdio.h>
#include <map>
#include <algorithm>
using namespace std;
#define nmax 1030
#define p 666013
long n, s, i, v[nmax], j, rez, poz, a, b, ra, rb;
map <int, int> ma1[p+10];

void citire()
{
	scanf("%ld %ld",&n,&s);
	for (i=1;i<=n;i++)
		scanf("%ld",&v[i]);
}

void rezolvare()
{
	ma1[(v[1]+v[2])%p][v[1]+v[2]]++;
	for (poz=3;poz<=n-1;poz++)
	{
		for (i=poz+1;i<=n;i++)
			if (s>v[poz]+v[i])
				rez+=ma1[(s-v[poz]-v[i])%p][s-v[poz]-v[i]];
		for (i=1;i<=poz-1;i++)
			ma1[(v[i]+v[poz])%p][v[i]+v[poz]]++;
	}
	printf("%ld",rez);
}

int main()
{
	freopen("oite.in","r",stdin);
	freopen("oite.out","w",stdout);
	citire();
	sort(v+1,v+1+n);
	rezolvare();
	return 0;
}