Cod sursa(job #1545214)

Utilizator SilviuIIon Silviu SilviuI Data 6 decembrie 2015 16:02:21
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <stdio.h>
#include <vector>
#define nmax 110
#define mod 109973
using namespace std;
int n,m,i,j,k,t[nmax];
vector < pair<int,vector <int> > > uhash[mod+10];
bool inhash(int x)
{
    int y=x%mod;
    for (unsigned int i=0;i<uhash[y].size();i++)
        if (uhash[y][i].first==x) return true;
    return false;
}
void addhash(int x,int i,int j,int k)
{
    int y=x%mod;
    pair<int,vector <int> > a;
    a.first=x; a.second.push_back(i);
    a.second.push_back(j); a.second.push_back(k);
    uhash[y].push_back(a);
}
void findprint(int x)
{
    int y=x%mod;
    for (unsigned int i=0;i<uhash[y].size();i++)
        if (uhash[y][i].first==x) {
            for (unsigned int j=0;j<uhash[y][i].second.size();j++)
                printf("%d ",uhash[y][i].second[j]);
    }
}
void print(int hash1,int hash2)
{
    findprint(hash1); findprint(hash2);
}
int main() {
freopen("loto.in","r",stdin);
freopen("loto.out","w",stdout);
scanf("%d %d",&n,&m);
for (i=1;i<=n;i++) scanf("%d",&t[i]);
for (i=1;i<=n;i++)
    for (j=i;j<=n;j++)
        for (k=j;k<=n;k++)
        if (t[i]+t[j]+t[k]<=m && !inhash(t[i]+t[j]+t[k])) {
            addhash(t[i]+t[j]+t[k],t[i],t[j],t[k]);
            if (inhash(m-(t[i]+t[j]+t[k]))) {
                print(t[i]+t[j]+t[k],m-(t[i]+t[j]+t[k])); return 0;
            }
        }
printf("-1");
return 0;
}