Cod sursa(job #2227138)

Utilizator vladm98Munteanu Vlad vladm98 Data 31 iulie 2018 13:13:54
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("loto.in");
ofstream fout("loto.out");

int v[101];
map <int, vector<int>> hashh[1000000];

void insertt(int a, int b, int c, int d){
    int modulo = a % 1000000;
    for(auto y: hashh[modulo])
        if(y.first == a) return;
    hashh[modulo][a].push_back(b);
    hashh[modulo][a].push_back(c);
    hashh[modulo][a].push_back(d);
}

bool isIn(int x, int i, int j, int k){
    int modulo = x % 1000000;
    for(auto current : hashh[modulo]){
        if(current.first == x){
            for(auto y : current.second){
                fout << v[y] << " " ;
            }
            fout << v[i] << " " << v[j] << " " << v[k];
            return true;
        }
    }
    return false;
}

int main(){
    int n;
    long long S;
    fin >> n >> S;
    for(int i = 1; i <= n; ++i){
        fin >> v[i];
    }
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            for(int k = 1; k <= n; ++k){
                long long s = 1LL * v[i] + v[j] + v[k];
                if(s > S)
                    continue;
                insertt(s, i, j, k);
                int dif = S - s;
                if(isIn(dif, i, j, k))
                    return 0;
            }
        }
    }
    fout << "-1";
    return 0;
}