Cod sursa(job #1218385)

Utilizator mariusn01Marius Nicoli mariusn01 Data 10 august 2014 20:24:24
Problema Cowfood Scor 36
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.02 kb
#include <cstdio>
#define MOD 3210121

int n, k, s, i, j, t, nr1, semn, tot, sum;
int d[10010][32];
int M[32], v[32][32];

inline int maxim(int a, int b) {
    return (a > b ? a : b);
}

int main() {
    FILE *f = fopen("cowfood.in","r");
    FILE *g = fopen("cowfood.out","w");
    fscanf(f,"%d %d %d",&k,&s,&n);
    for (i=0;i<n;i++)
        for (j=1;j<=k;j++)
            fscanf(f,"%d",&v[i][j]);
    //D[i][j] = in cate moduri pot pune exact i bile identice in exact j cutii
    //in cutia j pot pune 0 bile si atunci conteaza D[i][j-1] sau cel putin o bila
    // caz in care bila i o pun in cutia j si atunci conteaza D[i-1][j]

    for (j=0;j<=s+1;j++)
        d[0][j] = 1;

    for (i=1;i<=s+1;i++)
        for (j=1;j<=k+1;j++)
            if (j == 1)
                d[i][j] = 1;
            else
                if (i==1)
                    d[i][j] = j;
                else
                    d[i][j] = (d[i-1][j] + d[i][j-1]) % MOD;
    //pentru a afla in cate moduri pot pune cel mult i bile in exact j cutii conteaza
    //d[i][j+1], adica pot pune in cutia j+1 0, 1, 2 ... i bile
    for (i=1;i<=((1<<n)-1);i++) {
        for (t=1;t<=k;t++) {
            M[t] = 0;
        }
        sum = 0;
        nr1 = 0;
        for (j=0;j<n;j++)
            if ((i>>j)&1) {
                // folosesc mixtura j
                nr1++;
                for (t=1;t<=k;t++)
                    M[t] = maxim(M[t], v[j][t]);
            }

//        if (nr1 == 1)
//            continue;

        for (t=1;t<=k;t++)
            sum += M[t];
        if (sum > s)
            continue;
        if (nr1 % 2 == 1) {
            semn = 1;
        } else {
            semn = -1;
        }

        //semn *= (-1);

        tot += (d[s-sum][k+1] * semn) % MOD;

        if (tot < 0)
            tot += MOD;
    }
    tot = d[s][k+1] - tot;
    if (tot < 0)
        tot += MOD;

    tot = tot - s * k - 1;

    if (tot < 0)
        tot += MOD;

    fprintf(g,"%d\n",tot);

    return 0;
}