Cod sursa(job #1799272)

Utilizator martonsSoos Marton martons Data 5 noiembrie 2016 23:51:46
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <fstream>

#define MOD 46563
using namespace std;

struct comb{
    int a, b, c;
    comb *next;
};

comb *h[MOD];

void add(comb *val){
    int pos = val->a + val->b + val->c;

    if(h[pos%MOD]==NULL){
        h[pos%MOD] = val;
    }
}

bool get(int sum, comb &c){
    if(h[sum] == NULL)return false;
    c = *h[sum];
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    ifstream in("loto.in");
    int n, s, v[100];

    in>>n>>s;
    for(int i=0;i<n;i++){
        in>>v[i];
    }

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            for(int k=0;k<n;k++){
                add(new comb{v[i], v[j], v[k], NULL});
            }
        }
    }

    ofstream out("loto.out");
    comb c;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            for(int k=0;k<n;k++){
                if(get(s-v[i]-v[j]-v[k], c)){
                    out<<v[i]<<" "<<v[j]<<" "<<v[k]<<" "<<c.a<<" "<<c.b<<" "<<c.c;
                    return 0;
                }
            }
        }
    }
    out<<-1;
    return 0;
}