Cod sursa(job #1413022)

Utilizator OnimushaLordTiberiu Copaciu OnimushaLord Data 1 aprilie 2015 18:08:03
Problema Vanatoare Scor 55
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
# include <cstdio>
# include <algorithm>

using namespace std;

# define MAX_N 16
# define f first
# define s second
# define mp make_pair
# define LL long long

int N, T, L[1<<MAX_N], Sol[1<<MAX_N];
pair<int, int> A[MAX_N], Best[1<<MAX_N];

int cmmdc(int a, int b)
{
    int aux;
    while(b)
    {
        aux=a%b;
        a=b;
        b=aux;
    }
    return a;
}

int solve(pair<int, int> a, pair<int, int> b)
{
    int returneaza;

    if ((a.s-b.s) % cmmdc(a.f, b.f) != 0) return -1;
    if (a.f < b.f) swap(a, b);
    for (returneaza = 0; (LL) returneaza+a.s <= T; returneaza += a.f)
        if ( (returneaza+a.s)%b.f == b.s) return returneaza+a.s;
    return -1;
}

int main()
{
    int i, j;

    freopen("vanatoare.in", "r", stdin);
    freopen("vanatoare.out", "w", stdout);

    scanf("%d %d", &N, &T);
    for (i = 0; i < N; ++i)
        scanf("%d %d", &A[i].s, &A[i].f);

    sort(A, A+N);


    for (L[0]=i=1; i<(1<<N); ++i)
    {
        for (j = N-1; j >= 0 && !(i&(1<<j)); --j);
        L[i] = min((LL)T+1, (LL)L[i-(1<<j)]*A[j].f/cmmdc(L[i-(1<<j)], A[j].f));


        if (Sol[i-(1<<j)] == -1)
            Sol[i] = -1;
        else
            Sol[i] = solve(mp(L[i-(1<<j)], Sol[i-(1<<j)]), mp(A[j].f, A[j].s));


        Best[i] = mp(N+1, -1);
        for (j=i; j>0; j=i&(j-1))
            if (Sol[j]!=-1)
                Best[i]=min(Best[i],mp(Best[i-j].f+1,j));
    }

    printf("%d\n", Best[(1<<N)-1].f);
    if(N<=13)
    {
        for (i = (1<<N)-1; i != 0; i -= Best[i].s)
        printf("%d ", Sol[Best[i].s]);
    }
    else printf("-1\n");

    return 0;
}