Cod sursa(job #3295653)

Utilizator Nasa1004Ema Nicole Gheorghe Nasa1004 Data 7 mai 2025 17:11:12
Problema Cowfood Scor 28
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.38 kb
#include <fstream>
#include <vector>

using namespace std;
const int NMAX = 20;
const int KMAX = 30;
const int SMAX = 10000;
const int MOD = 3210121;
using ll = long long;

ifstream cin("cowfood.in"); ///back to farmer john I see
ofstream cout("cowfood.out");

ll logexp(ll a, int n) {
    ll p = 1;
    for(int k = 1; k <= n; k <<= 1) {
        if(n & k)
            p *= a;
        a *= a;
        p %= MOD;
        a %= MOD;
    }
    return p;
}

ll fact[SMAX + KMAX + 2], invmod[SMAX + KMAX + 2];
void init() {
    fact[0] = 1;
    for(int i = 1; i <= SMAX + KMAX; i++)
        fact[i] = (fact[i - 1] * i) % MOD;
    invmod[SMAX + KMAX] = logexp(fact[SMAX + KMAX], MOD - 2);
    for(int i = SMAX + KMAX - 1; i >= 0; i--)
        invmod[i] = (invmod[i + 1] * (i + 1)) % MOD;
}

ll comb(int n, int k) {
    if(n < k)
        return 0;
    if(n == k || k == 0)
        return 1;
    if(k == 1)
        return n;
    ll a = fact[n];
    ll b = (fact[k] * fact[n - k]) % MOD;
    return (a * logexp(b, MOD - 2)) % MOD;
    //return (1LL * fact[n] * invmod[k] * invmod[n - k]) % MOD;
}

int a[NMAX + 2][KMAX + 2];
int curr[KMAX + 2];
int mars[SMAX + 2];
signed main() {
    int k, s, n;
    cin >> k >> s >> n;
    for(int i = 0; i < n; i++) ///de la 0, ca mask
        for(int j = 1; j <= k; j++)
            cin >> a[i][j];

    init();
    mars[0]++; ///de cate ori adunam comb --> asta e pt nr TOTAL de comb
    mars[s + 1]--;
    for(int mask = 1; mask < (1 << n); mask++) {
        int nrbiti = __builtin_popcount(mask);
        int sum = 0;
        for(int bit = 0; bit < n; bit++) {
            if((1 << bit) & mask) {
                for(int j = 1; j <= k; j++)
                    curr[j] = max(curr[j], a[bit][j]);
            }
        }
        for(int i = 1; i <= k; i++) {
            sum += curr[i];
            curr[i] = 0;
        }
        if(sum > s) ///deja e gresit
            continue;
        if(nrbiti % 2 == 1) {
            mars[0]--;
            mars[s - sum + 1]++;
        }
        else {
            mars[0]++;
            mars[max(s, s - sum) + 1]--;
        }
    }
    ll tot = 0;
    for(int i = 0; i <= s; i++) {
        if(i > 0)
            mars[i] += mars[i - 1];
        ll nr = (mars[i] * comb(i + k - 1, k - 1)) % MOD;
        tot = (tot + nr) % MOD;
        //cout << mars[i] << " ";
    }
    tot = (tot - (s * k + 1)) % MOD;
    if(tot < 0)
        tot += MOD;
    cout << tot;
    return 0;
}