Cod sursa(job #1733524)

Utilizator cubaLuceafarul cuba Data 24 iulie 2016 20:40:10
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.94 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("loto.in");
ofstream g("loto.out");
const int MOD = 100003;
const int NMAX = 103;
struct Hash {
    vector < int > v[MOD];
    inline void Add(const int x) {
        if(Search(x))
            return;
        v[x%MOD].push_back(x);
    }
    inline bool Search(const int x) {
        int rest = x % MOD;
        int sz = v[rest].size();
        for(int i = 0 ; i < sz; i++) {
            if(v[rest][i] == x)
                return 1;
        }
        return 0;
    }
    inline void Delete(const int x) {
        int rest = x % MOD;
        int sz = v[rest].size();
        for(int i = 0 ; i < sz; i++) {
            if(v[rest][i] == x) {
                swap(v[rest][i],v[rest][sz-1]);
                v[rest].pop_back();
                return;
            }
        }
    }

};
Hash H;
int a[NMAX];
int main()
{
    int n , sum, cealalta;
    f >> n >> sum;
    for(int i = 1; i <= n ; i++) {
        f>> a[i];
    }
    bool stop = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = i; j <= n; j++) {
            for(int k = j; k <= n && stop == 0; k++) {
                int s = a[i] + a[j] + a[k];
                H.Add(s);
                if(s < sum)
                {
                    if(H.Search(sum - s))
                    {
                        g<< a[i] << " " << a[j] << " " << a[k] << " ";
                        cealalta = sum - s;
                        stop = 1;
                    }
                }
            }
        }
    }
    if(!stop) {
        g<< "-1";
        return 0;
    }
    for(int i = 1; i <= n; i++) {
        for(int j = i; j <= n; j++) {
            for(int k = j; k <= n; k++) {
                if(cealalta == a[i] + a[j] + a[k]) {
                    g<< a[i] << " " << a[j] << " " << a[k] << " ";
                    return 0;
                }
            }
        }
    }
    return 0;
}