Cod sursa(job #416193)

Utilizator indestructiblecont de teste indestructible Data 12 martie 2010 12:46:41
Problema Oite Scor 90
Compilator cpp Status done
Runda Maxim #1 Marime 1.29 kb
#include <stdio.h>
#include <algorithm>
#include <vector>
#define NMAX 1035
#define LMAX (1<<20)+1
#define MOD 666013
#define HMAX 12001
#define ll long long
using namespace std;
int n,m,A[NMAX],r,L[MOD];
struct suma
{
	int a,b,c;
};
suma B[LMAX];
vector <int> C[MOD];
ll rez;
char D[HMAX];
inline int cif(char x)
{
	return x>='0' && x<='9';
}
void read()
{
	scanf("%d%d\n",&n,&m);
	int i,j,poz=0;
	fgets(D+1,HMAX,stdin);
	for (i=1; i<=n; i++)
	{
		while (cif(D[poz+1]))
		{
			poz++;
			A[i]=A[i]*10+D[poz]-'0';
		}
		while (!cif(D[poz+1]))
			poz++;
	}
	sort(A+1,A+n+1);
}
void precompute()
{
	int i,j,t,rest;
	for (i=1; i<=n; i++)
		for (j=i+1; j<=n; j++)
		{
			t=A[i]+A[j];
			if (t<=m)
			{
				rest=t%MOD;
				B[++r].a=t; B[r].b=i; B[r].c=j;
				C[rest].push_back(r);
			}
		}
}
void get_size()
{
	int i;
	for (i=0; i<MOD; i++)
		L[i]=C[i].size();
}
void solve()
{
	int i,j,t,s,rest,k;
	for (i=1; i<=n; i++)
	{
		for (j=i+1; j<=n; j++)
		{
			t=A[i]+A[j];
			if (t<=m)
			{
				t=m-t;
				rest=t%MOD;
				for (k=0; k<L[rest]; k++)
				{
					s=C[rest][k];
					if (B[s].a==t)
						if (B[s].b>j)
							rez++;
				}
			}
		}
	}
}
int main()
{
	freopen("oite.in","r",stdin);
	freopen("oite.out","w",stdout);
	read();
	precompute();
	get_size();
	solve();
	printf("%lld\n",rez);
	return 0;
}