Cod sursa(job #2739655)

Utilizator dascalu_maraDascalu Mara Elena dascalu_mara Data 9 aprilie 2021 10:10:40
Problema Loto Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.52 kb
//
//  main.cpp
//  Loto
//
//  Created by Mara Dascalu on 07/04/2021.
//

#include <iostream>
#include <fstream>
#include <vector>
#include <tuple>
using namespace std;

ifstream input("loto.in");
ofstream output("loto.out");

const int val_hash = 666013;
vector<tuple<int, int, int, int>> v_hash[val_hash];
int nr, suma;
int v[101];
bool gasit;

int main( )
{
    input>>nr>>suma;
    for( int i = 0; i < nr; i++)
        input>>v[i];
    sort(v, v + nr);
    
    for( int i = 0; i < nr ; i++)
        for (int j = i; j < nr ; j++)
            for (int k = j; k < nr; k++)
                if (v[i] + v[j] + v[k] < suma)
                {
                    int s = v[i] + v[j] + v[k];
                    int poz  = s%val_hash;
                    v_hash[poz].push_back(make_tuple(s, v[i], v[j], v[k]));
                }
    
    
    for( int i = 0; i < nr & !gasit; i++)
        for(int j = i; j < nr & !gasit; j++)
            for(int k = j; k < nr & !gasit; k++)
            if (v[i] + v[j] + v[k] < suma)
            {
                int s = v[i] + v[j] + v[k];
                s = suma - s;
                int poz = s%val_hash;
                for (int l = 0; l < v_hash[poz].size(); l++)
                {
                    int val  = get<0>(v_hash[poz][l]);
                    if (s == val)
                        output<<v[i]<<" "<<v[j]<<" "<<v[k]<<" "<<get<1>(v_hash[poz][l])<<" "<<get<2>(v_hash[poz][l])<<" "<<get<3>(v_hash[poz][l]);
                    gasit = 1;
                }
            }
    if( !gasit) output<<"-1";
    
}