Cod sursa(job #3123933)

Utilizator Andrei_ierdnANeculau Rares-Andrei Andrei_ierdnA Data 26 aprilie 2023 13:28:29
Problema Cowfood Scor 60
Compilator cpp-64 Status done
Runda Lista lui wefgef Marime 1.78 kb
#include <fstream>

using namespace std;

ifstream f("cowfood.in");
ofstream g("cowfood.out");

#define MAXK 30LL
#define MAXS 10000LL
#define MOD 3210121LL

long long k, s, n, i, j, a[22][32], a2[32], comb[10100][31], mask, aux, x, ans, suma, nr;

int main()
{
    for (i = 0; i <= MAXK; i++) {
        comb[i][i] = 1;
    }
    for (i = 0; i <= MAXK+MAXS+1; i++) {
        comb[i][0] = 1;
    }
    for (i = 1; i <= MAXK+MAXS+1; i++) {
        for (j = 1; j <= min(i-1, MAXK); j++) {
            comb[i][j] = (comb[i-1][j] + comb[i-1][j-1]) % MOD;
        }
    }
    f >> k >> s >> n;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= k; j++) {
            f >> a[i][j];
        }
    }
    for (mask = 1; mask < (1LL << n); mask++) {
        for (i = 1; i <= k; i++) {
            a2[i] = 0;
        }
        x = -1;
        for (i = 0; i < n; i++) {
            if (mask & (1 << i)) {
                x *= -1;
                for (j = 1; j <= k; j++) {
                    a2[j] = max(a2[j], a[i+1][j]);
                }
            }
        }
        nr = 0;
        suma = 0;
        for (i = 1; i <= k; i++) {
            suma += a2[i];
            nr += (a2[i] > 0);
        }
        if (suma > s) {
            continue;
        }
        suma = s-suma;
        if (nr >= 2) {
            aux = comb[suma+k][k];
        }
        else if (nr == 1) {
            aux = comb[suma+k][k] - (suma+1);
        }
        else {
            aux = comb[suma+k][k] - k * suma - 1;
        }
        ans = (ans + x * aux) % MOD;
    }
    suma = s;
    aux = (comb[suma+k][k] - k * suma - 1) % MOD;
    ans = aux - ans;
    ans %= MOD;
    if (ans < 0)
        ans += MOD;
    g << ans;
    f.close();
    g.close();
    return 0;
}