Cod sursa(job #1190600)

Utilizator romircea2010FMI Trifan Mircea Mihai romircea2010 Data 25 mai 2014 15:03:21
Problema Loto Scor 30
Compilator cpp Status done
Runda itmarathon Marime 1.76 kb
#include <fstream>
#include <vector>

using namespace std;

const int NMax = 102, SMax = 600000000, MOD = 666013;

struct numar
{
    int value, i, j, k;
    numar() {value = i = j = k = 0;}
    numar(const int value, const int i, const int j, const int k)
    {
        this -> value = value;
        this -> i = i;
        this -> j = j;
        this -> k = k;
    }
    bool operator < (const numar & other) const
    {
        return value < other.value;
    }
};
int N, S;
int a[NMax];
vector <numar> H[MOD];
int nv;

int main()
{
    ifstream f ("loto.in");
    f >> N >> S;
    for (int i = 1; i <= N; ++ i)
        f >> a[i];
    f.close();
    for (int i = 1; i <= N; ++ i)
        for (int j = 1; j <= N; ++ j)
            for (int k = 1; k <= N; ++ k)
            {
                int sum = a[i] + a[j] + a[k];
                H[sum % MOD].push_back(numar(sum, i, j, k));
            }
    for (int cod = 0; cod < MOD; ++ cod)
        for (vector <numar> :: iterator it = H[cod].begin(); it != H[cod].end(); ++ it)
        {
            numar aux = *it;
            int ns = S - aux.value;
            int newcod = ns % MOD;
            for (vector <numar> :: iterator jt = H[newcod].begin(); jt != H[newcod].end(); ++ jt)
            {
                numar newaux = *jt;
                if (newaux.value == ns)
                {
                    ofstream g("loto.out");
                    g << a[aux.i] << " " << a[aux.j] << " " << a[aux.k] <<" ";
                    g << a[newaux.i] << " " << a[newaux.j] << " " << a[newaux.k] << "\n";
                    g.close();
                    return 0;
                }
            }
        }
    ofstream g("loto.out");
    g << "-1\n";
    g.close();
    return 0;
}