Cod sursa(job #809548)

Utilizator matei_cChristescu Matei matei_c Data 8 noiembrie 2012 17:40:40
Problema Loto Scor 65
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std ;

#define maxn 101
#define mod 2000003

struct aaa
{
	int suma, a, b, c ; 
	
};	

int n, s ;
int a[maxn] ;
vector <aaa> hash[mod] ; 

int main()
{
	
	freopen("loto.in", "r", stdin);
	freopen("loto.out", "w", stdout);
	
	scanf("%d%d", &n, &s);
	
	for( int i = 1 ; i <= n ; ++ i )
		scanf("%d", &a[i]);
	
	for( int i = 1 ; i <= n ; ++ i )
	{
		for( int j = 1 ; j <= n ; ++ j )
		{
			for( int k = 1 ; k <= n ; ++ k )
			{
				
				int sum = a[i] + a[j] + a[k] ;
				if( sum > s )
					continue ;
				int r = sum % mod ;
				aaa abc;
				abc.suma = sum;
				abc.a = a[i] ;
				abc.b = a[j] ;
				abc.c = a[k] ;
				hash[r].push_back( abc ) ;
			
			}	
		}	
	}
	
	for( int i = 0 ; i < mod ; ++ i )
	{
		for( size_t j = 0 ; j < hash[i].size() ; ++ j )
		{
			
			int scaut = s - hash[i][j].suma ;
			if( scaut < 0 )
				continue ;
			int rcaut = scaut % mod ;
			
			for( size_t k = 0 ; k < hash[rcaut].size() ; ++ k )
			{
				
				if( hash[rcaut][k].suma == scaut )
				{
					printf("%d %d %d ", hash[i][j].a, hash[i][j].b, hash[i][j].c);
					printf("%d %d %d\n",hash[rcaut][k].a, hash[rcaut][k].b, hash[rcaut][k].c);
					return 0 ;
				}
				
			}
			
		}	
	}	
	
	printf("-1\n");
	
	return 0;
	
}