Cod sursa(job #1604482)

Utilizator LuurAndrei Florea Luur Data 18 februarie 2016 12:38:57
Problema Loto Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#include <fstream>
#define DMAX 1000100
#define TMAX 101
#define K 393241
using namespace std;
typedef long  ll;
ifstream in("loto.in");
ofstream out("loto.out");
int n;
long S, a[TMAX], m;
bool t = 1;
struct myType{
    int a, b, c;
    ll s;
    bool operator == (ll const & x) const{
        return s == x;
    }
};
typedef vector<myType> vm;
vm v[K];
bool sortOp(myType x, myType y){
   return x.s < y.s;
}
inline ll h(ll x){
    return x % K;
}
inline myType Find(ll x){
     ll y = h(x);
     bool t = 1;
     myType T; T.s = -1;
     for(unsigned int i = 0; i < v[y].size() && t; i++){
        T = v[y][i];
        if (T == x) t = 0;
     }
    return T;
}
int main(){
    in >> n >> S;
    for(int i = 1; i <= n; i++){
        in >> a[i];
    }
    for(int i = 1; i <= n; i++){
        for(int j = i; j <= n; j++){
            for(int k = j; k <= n; k++){
                long long STemp = a[i] + a[j] + a[k];
                if (STemp <= S){
                    myType T;
                    T.a = a[i]; T.b = a[j]; T.c = a[k]; T.s = STemp;
                    v[h(STemp)].push_back(T);
                }
            }
        }
    }
    for(int i = 1; i <= n && t; i++){
        for(int j = i; j <= n && t; j++){
            for(int k = j; k <= n && t; k++){
                ll c = S - a[i]-a[j]-a[k];
                if (c >= 0){
                    myType it = Find(c);
                    if (it.s != -1){
                        out <<a[i]<<' '<<a[j]<<' '<<a[k]<<' '<<it.a<<' '<<it.b<<' '<<it.c<<'\n';
                        t = 0;
                    }
                }
            }
        }
    }
    if (t) out <<-1<<'\n';
    return 0;
}