Cod sursa(job #498663)

Utilizator stay_awake77Cangea Catalina stay_awake77 Data 5 noiembrie 2010 18:25:50
Problema Loto Scor 65
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <stdio.h>

struct point {long long inf,x1,x2,x3; point *leg;};

long long n,s,elem[100],i,j,k,suma;
point *sume[1100001],*p,*pcompl;

void insereaza(long long numar)
{
	point *nou,*aa=sume[numar%1100000];
	
	nou=aa;
	while (nou!=NULL)
	{
		if (nou->inf==numar) return;
		nou=nou->leg;
	}
	
	nou=new point;
	nou->inf=numar;
	nou->x1=elem[i]; nou->x2=elem[j]; nou->x3=elem[k];
	nou->leg=sume[numar%1100000]; 
	sume[numar%1100000]=nou;
}

point *cauta(long long numarr)
{
	point *aa=sume[numarr%1100000];
	
	while (aa!=NULL)
	{
		if (aa->inf==numarr) return aa;
		aa=aa->leg;
	}
	
	return NULL;
}

int main()
{
	freopen("loto.in","r",stdin);
	freopen("loto.out","w",stdout);
	
	scanf("%lld%lld",&n,&s);
	for (i=0;i<n;i++)
		scanf("%lld",&elem[i]);
	
	for (i=0;i<n;i++)
		for (j=0;j<n;j++)
			for (k=0;k<n;k++)
			{
				suma=elem[i]+elem[j]+elem[k];
				insereaza(suma);
			}
	
	for (i=0;i<1100000;i++)
	{
		p=sume[i];
		while (p)
		{
			suma=p->inf;
			pcompl=cauta(s-suma);
			if (pcompl!=NULL)
			{
				printf("%lld %lld %lld %lld %lld %lld\n",p->x1,p->x2,p->x3,pcompl->x1,pcompl->x2,pcompl->x3);
				return 0;
			}
			p=p->leg;
		}
	}
	
	printf("-1\n");
	
	return 0;
}