Cod sursa(job #1514111)

Utilizator theprdvtheprdv theprdv Data 30 octombrie 2015 16:29:01
Problema Loto Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <stdio.h>
#include <assert.h>
#include <string.h>

#define MAXN 128
#define MOD 666013

struct hash_line
{
    int no1, no2, no3;
    struct hash_line *link;
} *Hash[MOD];

int N, S, A[MAXN];

inline struct hash_line *find(int no1, int no2, int no3)
{
    int sum = A[no1] + A[no2] + A[no3], key = sum % MOD, req = S - sum;
    if (req <= 0) return 0;

    struct hash_line *it = Hash[req];

    for (; it; it = it->link)
        if (A[it->no1] + A[it->no2] + A[it->no3] == req)
            return it;

    struct hash_line *New = malloc(sizeof(New));
    New->no1 = no1, New->no2 = no2, New->no3 = no3;
    New->link = Hash[key];
    Hash[key] = New;

    return  0;
}

int main()
{
    assert(freopen("loto.in", "r", stdin));
    freopen("loto.out", "w", stdout);

    int i, j, k; // local variables
    struct hash_line *now = NULL;

    scanf("%d %d", &N, &S);
    for (i = 0; i < N; ++i) scanf("%d", &A[i]);

    for (i = 0; i < N; ++i)
        for (j = 0; j < N; ++j)
            for (k = 0; k < N; ++k)
            if ( (now = find(i, j, k)) )
            {
                printf("%d %d %d %d %d %d", A[i], A[j], A[k], A[now->no1], A[now->no2], A[now->no3]);
                return 0;
            }

    printf("-1");
    return 0;
}