Cod sursa(job #1105706)

Utilizator tzipleatudTudor Tiplea tzipleatud Data 11 februarie 2014 23:49:10
Problema Cowfood Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <fstream>
#define NM 35
#define SM 10010

using namespace std;

ifstream f("cowfood.in");
ofstream g("cowfood.out");

const int MOD=3210121;
int N, K, S, cnt;
int A[NM][NM], B[NM][NM];
int DP[NM][SM];
int ANS;

void Back (int p)
{
    if (p>N)
    {
        int NS=S;
        for (int j=1; j<=K; j++)
            NS-=B[cnt][j];
        if (NS<0)
            return;

        if (cnt%2)
        {
            ANS+=MOD-DP[K][NS];
            if (ANS>=MOD)
                ANS-=MOD;
        }
        else
        {
            ANS+=DP[K][NS];
            if (ANS>=MOD)
                ANS-=MOD;
        }

        return;
    }

    Back(p+1);

    cnt++;
    for (int j=1; j<=K; j++)
        B[cnt][j]=max(B[cnt-1][j], A[p][j]);
    Back(p+1);
    cnt--;
}

int main ()
{
    f >> K >> S >> N;
    for (int i=1; i<=N; i++)
        for (int j=1; j<=K; j++)
            f >> A[i][j];

    DP[0][0]=1;
    for (int j=0; j<=S; j++)
        DP[1][j]=DP[1][j-1]+1;

    for (int i=2; i<=K; i++)
    {
        DP[i][0]=1;
        for (int j=1; j<=S; j++)
        {
            DP[i][j]=DP[i-1][j] + DP[i][j-1];
            if (DP[i][j]>=MOD)
                DP[i][j]-=MOD;
        }
    }

    Back(1);

    g << (ANS-S*K-1+MOD)%MOD << '\n';

    f.close();
    g.close();

    return 0;
}