Cod sursa(job #962874)
Utilizator | Data | 15 iunie 2013 20:51:42 | |
---|---|---|---|
Problema | Loto | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 4.67 kb |
#include <fstream>
#include <algorithm>
using namespace std;
long N,S;
long A[105];
char BitMask[37500005];
int main(void)
{
fstream fin("loto.in",ios::in);
fstream fout("loto.out",ios::out);
fin >> N >> S;
for (long a = 0;a < N;a += 1)
{
fin >> A[a];
}
if (N == 1)
{
if ((A[0] * 6) == S)
{
fout << A[0] << " " << A[0] << " " << A[0] << " " << A[0] << " " << A[0] << " " << A[0];
}
else
{
fout << "-1";
}
}
if (N == 2)
{
long ok = 0;
long a = 0;
long b = 1;
for (long c = 0;c <= 6;c += 1)
{
if (((c * A[a]) + ((6 - c) * A[b])) == S)
{
ok = 1;
long d;
for (d = 0;d < c;d += 1)
{
fout << A[a] << " ";
}
for (;d < 6;d += 1)
{
fout << A[b] << " ";
}
break;
}
}
if (ok == 0)
{
fout << "-1";
}
}
if (N > 2)
{
long ok = 0;
long K;
long Best;
for (long a = 0;a < N;a += 1)
{
for (long b = a;b < N;b += 1)
{
for (long c = b;c < N;c += 1)
{
long S = A[a] + A[b] + A[c];
BitMask[S >> 3] |= (1 << (S & 0X7));
}
}
}
if (ok == 0)
{
for (long a = 0;a < N;a += 1)
{
K = S - (3 * A[a]);
if ((K > 300000000) || (K < 0))
{
continue;
}
if ((BitMask[K >> 3] & (1 << (K & 0X7))) != 0)
{
fout << A[a] << " " << A[a] << " " << A[a] << " ";
Best = K;
ok = 1;
goto found;
}
}
}
if (ok == 0)
{
for (long a = 0;a < N;a += 1)
{
for (long b = 0;b < N;b += 1)
{
for (long c = 0;c <= 3;c += 1)
{
K = S - (c * A[a]) - ((3 - c) * A[b]);
if ((K > 300000000) || (K < 0))
{
continue;
}
if ((BitMask[K >> 3] & (1 << (K & 0X7))) != 0)
{
long d;
for (d = 0;d < c;d += 1)
{
fout << A[a] << " ";
}
for (;d < 3;d += 1)
{
fout << A[b] << " ";
}
Best = K;
ok = 1;
goto found;
}
}
}
}
}
if (ok == 0)
{
for (long a = 0;a < N;a += 1)
{
for (long b = a;b < N;b += 1)
{
for (long c = b;c <= N;c += 1)
{
K = S - A[a] - A[b] - A[c];
if ((K > 300000000) || (K < 0))
{
continue;
}
if ((BitMask[K >> 3] & (1 << (K & 0X7))) != 0)
{
fout << A[a] << " " << A[b] << " " << A[c] << " ";
Best = K;
ok = 1;
goto found;
}
}
}
}
}
found:;
if (ok == 0)
{
fout << "-1";
}
else
{
for (long a = 0;a < N;a += 1)
{
for (long b = a;b < N;b += 1)
{
for (long c = b;c < N;c += 1)
{
if (Best == (A[a] + A[b] + A[c]))
{
fout << A[a] << " " << A[b] << " " << A[c];
goto finish;
}
}
}
}
}
finish:;
}
fin.close();
fout.close();
return 0;
}