Cod sursa(job #2808580)

Utilizator DordeDorde Matei Dorde Data 25 noiembrie 2021 12:11:59
Problema Cowfood Scor 4
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin ("cowfood.in");
ofstream fout ("cowfood.out");
int const mod = 3210121 , N = 2e4 + 7;
int k , s , n , fact [N] , val [21][31] , v [31];
void getfact (){
    fact [0] = 1;
    for(int i = 1 ; i < N ; ++ i)
        fact [i] = 1LL * i * fact [i - 1] % mod;
}
int lgp (int a , int b){
    int r (1) , c (a);
    for(int j = 0 ; (1 << j) <= b ; ++ j){
        if ((1 << j) & 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) * lgp (fact [n - k] , mod - 2) % mod;
}
int main()
{
    fin >> k >> s >> n;
    int total (0);
    getfact ();
    for(int i = k - 1 ; i < s ; ++ i){
        total = total + C (i , k - 1);
        if (total >= mod)
            total -= mod;
    }
    for(int i = 0 ; i < n ; ++ i)
        for(int j = 0 ; j < k ; ++ j)
            fin >> val [i][j];
    for(int i = 1 ; i < (1 << n) ; ++ i){
        fill (v , v + k , 0);
        int c (0);
        for(int j = 0 ; (1 << j) <= i ; ++ j){
            ///iau experimentu j
            if ((1 << j) & i){
                ++ c;
                for(int y = 0 ; y < k ; ++ y)
                    v [y] = max (v [y] , val [j][y]);
            }
        }
        int sum (0);
        for(int y = 0 ; y < k ; ++ y)
            sum += v [y];
        if (sum > s)
            continue;
        total += ((C (s - sum - 1 + k , k - 1) + 1) * (c % 2 ? -1 : 1));
        if (total < 0)
            total += mod;
        if (total >= mod)
            total -= mod;
    }
    fout << total << '\n';
    return 0;
}