Cod sursa(job #65657)

Utilizator gigi_becaliGigi Becali gigi_becali Data 11 iunie 2007 12:45:31
Problema Loto Scor 35
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
using namespace std;
#include <algorithm>
#include <cstdio>
#define maxn 128
#define maxh 10001
struct nod { int x, y, z; nod *next;};
nod *H[maxh];
int N, S;

void insert(int x, int y, int z)
{
	int s=x+y+z;
	int h=s%maxh;
	for(nod *p=H[h];p;p=p->next)
		if(p->x+p->y+p->z==s) return;
	
	nod *t=new nod;
	t->x=x;
	t->y=y;
	t->z=z;
	t->next=H[h];
	H[h]=t;
}



int main()
{
	freopen("loto.in", "r", stdin);
	freopen("loto.out", "w",stdout);
	scanf("%d %d\n", &N, &S);
	int i, j, k;
	int x[maxn];
	for(i=1;i<=N;++i) scanf("%d ", x+i); 

	
	for(i=1;i<=N;++i)
		for(j=1;j<=N;++j)
			for(k=1;k<=N;++k)
				insert(x[i], x[j], x[k]);
			
	int s, h;
	nod *p;
	for(i=1;i<=N;++i)
		for(j=1;j<=N;++j)
			for(k=1;k<=N;++k)
			{
				s=S-(x[i]+x[j]+x[k]);
				h=s%maxh;
				for(p=H[h];p;p=p->next)
					if(p->x+p->y+p->z==s)
					{
						int st[10], t=0;
						st[++t]=x[i];
						st[++t]=x[j];
						st[++t]=x[k];
						st[++t]=p->x;
						st[++t]=p->y;
						st[++t]=p->z;
						sort(st+1, st+t+1);
						for(int r=1;r<=t;++r) printf("%d ", st[r]);
						printf("\n");
						return 0;
					}
			}
		printf("-1\n");
		
	
	return 0;
}