Cod sursa(job #1741822)

Utilizator hanganflorinHangan Florin hanganflorin Data 15 august 2016 11:31:45
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.39 kb
#include <fstream>
using namespace std;

ifstream is("loto.in");
ofstream os("loto.out");

int n, S, s, a[101], x[7];
bool gasit = false;
void Comb(int k);
void Write();

int main()
{
    is >> n >> S;
    for ( int i = 1; i <= n; ++i )
        is >> a[i];
    // 6 fucking for-uri
    // why not
    for ( int i1 = 1; i1 <= n && !gasit; ++i1 )
    {
        s += a[i1];
        x[1] = a[i1];
        if ( s > S )
            continue;
        for ( int i2 = 1; i2 <= n && !gasit; ++i2 )
        {
            s += a[i2];
            x[2] = a[i2];
            if ( s > S )
                continue;
            for ( int i3 = 1; i3 <= n && !gasit; ++i3 )
            {
                s += a[i3];
                x[3] = a[i3];
                if ( s > S )
                    continue;
                for ( int i4 = 1; i4 <= n && !gasit; ++i4 )
                {
                    s += a[i4];
                    x[4] = a[i4];
                    if ( s > S )
                        continue;
                    for ( int i5 = 1; i5 <= n && !gasit; ++i5 )
                    {
                        s += a[i5];
                        x[5] = a[i5];
                        if ( s > S )
                            continue;
                        for ( int i6 = 1; i6 <= n && !gasit; ++i6 )
                        {
                            s += a[i6];
                            x[6] = a[i6];
                            if ( s > S )
                                continue;
                            if ( s == S )
                                Write();
                            s -= a[i6];
                        }
                        s -= a[i5];
                    }
                    s -= a[i4];
                }
                s -= a[i3];
            }
            s -= a[i2];
        }
        s -= a[i1];
    }
    if ( !gasit )
        os << -1;
    is.close();
    os.close();
    return 0;
}
void Comb(int k)
{
    if ( s > S )
        return;
    if ( k > 6 )
    {
        if ( s == S )
            Write();
        return;
    }
    for ( int i = x[k-1]; i <= n; ++i )
    {
        x[k] = i;
        s += a[i];
        Comb(k+1);
        if ( gasit )
            return;
        s -= a[i];

    }
}
void Write()
{
    gasit = true;
    for ( int i = 1; i <= 6; ++i )
        os << x[i] << ' ';
}