Cod sursa(job #1524625)

Utilizator yololy97Olaru Bogdan-Ioan yololy97 Data 14 noiembrie 2015 12:12:08
Problema Loto Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <cstdio>
#include <vector>
#define N 100
#define maxh 666013
using namespace std;
int v[N], n, S;
struct triple{
	int x, y, z;
};
vector<triple> hash_table[maxh];
void read(){
	scanf("%d %d\n", &n, &S);
	for(int i = 1; i <= n; ++i)
		scanf("%d ", &v[i]);
}
int hashfunction(triple a){
	return (a.x + a.y + a.z) % maxh;
}
void inser(triple a){
	int h = hashfunction(a);
	hash_table[h].push_back(a);
}
vector<triple>::iterator find(int s){
	int h = s % maxh;
	for(vector<triple>::iterator it = hash_table[h].begin(); it != hash_table[h].end(); ++it)
		if(it->x + it->y + it->z == s)
			return it;
	return hash_table[h].end();
}
void solve(){
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j)
			for(int k = 1; k <= n; ++k){
				triple a;
				a.x = v[i];
				a.y = v[j];
				a.z = v[k];
				inser(a);
			}
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j)
			for(int k = 1; k <= n; ++k){
				int h = (S - v[i] - v[j] - v[k]) % maxh;
				vector<triple>::iterator it = find(S - v[i] - v[j] - v[k]);
				if(it != hash_table[h].end()){
					printf("%d %d %d %d %d %d\n", v[i], v[j], v[k], it->x, it->y, it->z);
					return;
				}
			}
	printf("-1\n");
}
int main(){
	freopen("loto.in", "r", stdin);
	freopen("loto.out", "w", stdout);
	read();
	solve();
}