Cod sursa(job #987386)

Utilizator manutrutaEmanuel Truta manutruta Data 20 august 2013 16:06:49
Problema Loto Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.79 kb
# include <iostream>
# include <fstream>
# include <vector>
# include <algorithm>
using namespace std;

# define MAXN 103
# define MOD 666013

ifstream f("loto.in");
ofstream g("loto.out");

struct punct{
    int sum;
    int a, b, c;
};

int n, s;
int a[MAXN];
vector<punct> hassh[MOD];

void addHash(punct x) {
    int aux = x.sum % MOD;
    hassh[aux].push_back(x);
}

bool existHash(int x) {
    int aux = x % MOD;
    for (int i = 0; i < hassh[aux].size(); i++) {
        if (hassh[aux][i].sum == x) {
            return true;
        }
    }
    return false;
}

punct getHash(int x) {
    int aux = x % MOD;
    for (int i = 0; i < hassh[aux].size(); i++) {
        if (hassh[aux][i].sum == x) {
            return hassh[aux][i];
        }
    }
}

int main()
{
    f >> n >> s;
    for (int i = 1; i <= n; i++) {
        f >> a[i];
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                punct aux;
                aux.sum = a[i] + a[j] + a[k];
                aux.a = a[i];
                aux.b = a[j];
                aux.c = a[k];
                addHash(aux);
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                int aux = s - a[i] - a[j] - a[k];
                if (s > 0) {
                    if (existHash(aux) == true) {
                        punct x = getHash(aux);
                        g << a[i] << ' ' << a[j] << ' ' << a[k] << ' ';
                        g << x.a << ' ' << x.b << ' ' << x.c;
                        return 0;
                    }
                }
            }
        }
    }
    g << -1;

    return 0;
}