Cod sursa(job #2809315)

Utilizator DordeDorde Matei Dorde Data 26 noiembrie 2021 17:35:40
Problema Cowfood Scor 54
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <fstream>

using namespace std;
ifstream fin ("cowfood.in");
ofstream fout ("cowfood.out");
int const N = 2e4 + 7 , mod = 3210121;
int k , s , n , v [21][31], now [31] , dp [N][31];
void add (int &x , int y){
    x += y;
    if (x >= mod)
        x -= mod;
    if (x < 0)
        x += mod;
}
void precalc (){
    dp [0][0] = 1;
    for(int i = 0 ; i < N - 1 ; ++ i){
        for(int j = 0 ; j <= min (k , i) ; ++ j){
            if (i)
                add (dp [i][j] , dp [i - 1][j]);
            if (i && j)
                add (dp [i][j] , dp [i - 1][j - 1]);
        }
    }
}
void read (){
    fin >> k >> s >> n;
    for(int i = 0 ; i < n ; ++ i)
        for(int j = 0 ; j < k ; ++ j)
            fin >> v [i][j];
}
int main()
{
    read ();
    precalc ();
    int total (0);
    add (total , dp [s + k][k] - k * s - 1);
    for(int i = 1 ; i < (1 << n) ; ++ i){
        int sum (0) , c (0);
        fill (now , now + k , 0);
        for(int j = 0 ; (1 << j) <= i ; ++ j)
            if ((1 << j) & i){
                ++ c;
                for(int o = 0 ; o < k ; ++ o){
                    sum -= now [o];
                    now [o] = max (now [o] , v [j][o]);
                    sum += now [o];
                }
            }
        if (sum > s)
            continue;
        int nr (dp [s - sum + k][k]);
        if (c % 2)
            add (total , -nr);
        else
            add (total , nr);
    }
    fout << total << '\n';
    fin.close ();
    fout.close ();
    return 0;
}