Cod sursa(job #2809279)

Utilizator DordeDorde Matei Dorde Data 26 noiembrie 2021 16:07:53
Problema Cowfood Scor 12
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.73 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] , fact [N] , cn [N] , now [31];
int lgp (int a , int b){
    int r (1) , c (a);
    for(int i = 0 ; (1 << i) <= b ; ++ i){
        if ((1 << i) & b)
            r = 1LL * r * c % mod;
        c = 1LL * c * c % mod;
    }
    return r;
}
int C (int n , int k){
    return 1LL * fact [n] * lgp (fact [k] , mod - 2) % mod * lgp (fact [n - k] , mod - 2) % mod;
}
void precalc (){
    fact [0] = 1;
    for(int i = 1 ; i < N - 1 ; ++ i)
        fact [i] = 1LL * fact [i - 1] * i % mod;
    for(int i = k - 1 ; i < N - 1 ; ++ i){
        cn [i] = cn [i - 1] + C (i , k - 1);
        if (cn [i] >= mod)
            cn [i] -= mod;
    }
}
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 (cn [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;
        total = total + (c % 2) * -1 * cn [s - sum + k - 1];
        if (total < 0)
            total += mod;
        if (total >= mod)
            total -= mod;
    }
    fout << total << '\n';
    fin.close ();
    fout.close ();
    return 0;
}