Cod sursa(job #2809333)

Utilizator DordeDorde Matei Dorde Data 26 noiembrie 2021 18:08:56
Problema Cowfood Scor 24
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.62 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] , cnow [31] , dp [N][31] , total;
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];
}
void bkt (int strat , int sum , int cnt , int ok){
    if (sum > s)
        return;
    if (strat == n){
        if (ok < 2)
            return;
        if (cnt & 1)
            add (total , -dp [s - sum + k][k]);
        else
            add (total , dp [s - sum + k][k]);
        return;
    }
    bkt (strat + 1 , sum , cnt , ok);
    copy (now , now + k , cnow);
    ok = 0;
    for(int i = 0 ; i < k ; ++ i)
        if (v [strat][i] > now [i]){
            sum += v [strat][i] - now [i];
            now [i] = v [strat][i];
            ok += (now [i] != 0);
        }
    bkt (strat + 1 , sum , cnt + 1 , ok);
    copy (cnow , cnow + k , now);

}
int main()
{
    read ();
    precalc ();
    add (total , dp [s + k][k] - k * s - 1);
    bkt (0 , 0 , 0 , 0);
    fout << total << '\n';
    fin.close ();
    fout.close ();
    return 0;
}