Cod sursa(job #494047)

Utilizator theodora_maneaManea Theodora Maria theodora_manea Data 20 octombrie 2010 16:38:51
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <stdio.h>
#include <string.h>

struct point {
	int inf;
	point *leg;
};
long a[100];
point *h[20000];
int n,n1,ok,i,j,k;
long s1,s2,s3,x;

void insert(long x,long k) {
	point *p;
	p=new point;
	p->inf=k;
	p->leg=h[x];
	h[x]=p;
}

int chash(long x, long s2) {
	int ok;
	ok=0;
	while (h[x]!=NULL) 
		if (s2==h[x]->inf) return 1;
	    else h[x]=h[x]->leg;
	return 0;
}

int cautare(int i) {
	int ok;
	ok=0;
	while (h[i]!=NULL) {
		s2=s1-(h[i]->inf);
		x=s2%n1;
		ok=chash(x,s2);
		if (ok) return 1;
	}
	return 0;
}

void afis() {
	int ok2,ok3;
    ok2=0; ok3=0;
    for (i=1; i<=n; i++)
        for (j=i; j<=n; j++)
            for (k=j; k<=n; k++) {
                if (ok2==0 || ok3==0) {
                if (a[i]+a[j]+a[k]==s2) {
                    printf("%ld %ld %ld",a[i],a[j],a[k]);
                    if (ok3==0) printf(" ");
                    else printf("\n");
                    ok2=1;
                }
                if (a[i]+a[j]+a[k]==s3) {
                    printf("%ld %ld %ld",a[i],a[j],a[k]);
                    if (ok2==0) printf(" ");
                    else printf("\n");
                    ok3=1;
						}
                }
                else break;
            }
}

int main () {
	freopen("loto.in","r",stdin);
	freopen("loto.out","w",stdout);
	scanf("%d%ld",&n,&s1);
	for (i=1; i<=n; i++) 
		scanf("%ld",&a[i]);
	n1=n/3;
	for (i=1; i<=n; i++) 
		for (j=i; j<=n; j++) 
			for (k=j; k<=n; k++) {
				s2=a[i]+a[j]+a[k];
			    x=s2%n1;
				insert(x,s2);
			}
	for (i=0; i<=n1; i++) {
		ok=0;
		ok=cautare(i);
		if (ok) {
			s3=s1-s2;
			afis();
			break;
		}
	}
	if (!ok) printf("-1");
	return 0;
}