Cod sursa(job #1218393)

Utilizator mariusn01Marius Nicoli mariusn01 Data 10 august 2014 21:01:07
Problema Cowfood Scor 48
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.76 kb
#include <cstdio>
#include <algorithm>
#include <fstream>
#define MOD 3210121

using namespace std;

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

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

void back (int K) {
    int MA[32];
    if (K == n) {

        if (nr1 == 0)
            return;

        sum = 0;

        for (int t=1;t<=k;t++)
            sum += M[t];
        if (sum > s)
            return;

        if (nr1 % 2 == 1) {
            semn = 1;
        } else {
            semn = -1;
        }

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

        if (tot < 0)
            tot += MOD;

    } else {
        for (int y = 0;y<=1;y++) {
            x[K] = y;

            if (y == 1) {
                for (int i=1;i<=k;i++) {
                    MA[i] = M[i];
                    M[i] = max(M[i], v[K][i]);
                }
                nr1++;
            }
            back(K+1);
            if (y == 1) {
                for (int i=1;i<=k;i++) {
                    M[i] = MA[i];
                }
                nr1--;
            }
        }
    }
}

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=0;j<=k+1;j++)
            if (j == 1 || j==0)
                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;


/*
    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) {
                nr1++;
                for (t=1;t<=k;t++)
                    M[t] = maxim(M[t], v[j][t]);
            }

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

        if (tot < 0)
            tot += MOD;
    }
*/
    back(0);

    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;
}