Cod sursa(job #3148582)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 2 septembrie 2023 19:00:13
Problema Cowfood Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.96 kb
#include <fstream>

using namespace std;

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

const long long max_size = 31, max_s = 1e4 + 1, max_n = 21, mod = 3210121;

long long a[max_n][max_size], dp[max_size][max_s], curr[max_n][max_size], n, k, ans, s;

void check (long long on)
{
    long long aux = 0, not0 = 0;
    for (long long i = 1; i <= k; i++)
    {
        aux += curr[n][i];
        if (curr[n][i] != 0)
        {
            not0++;
        }
    }
    if (aux > s || not0 < 2)
    {
        return;
    }
    if (on % 2 == 1)
    {
        ans = (ans + dp[k][s - aux]) % mod;
    }
    else
    {
        ans -= dp[k][s - aux];
        if (ans < 0)
        {
            ans += mod;
        }
        ans %= mod;
    }
}

void bkt (long long ct, long long on)
{
    for (long long i = 1; i <= k; i++)
    {
        curr[ct][i] = curr[ct - 1][i];
    }
    if (ct == n)
    {
        check(on);
    }
    else
    {
        bkt(ct + 1, on);
    }
    for (long long i = 1; i <= k; i++)
    {
        curr[ct][i] = max(curr[ct - 1][i], a[ct][i]);
    }
    if (ct == n)
    {
        check(on + 1);
    }
    else
    {
        bkt(ct + 1, on + 1);
    }
}

signed main ()
{
    in >> k >> s >> n;
    for (long long i = 1; i <= n; i++)
    {
        for (long long j = 1; j <= k; j++)
        {
            in >> a[i][j];
        }
    }
    dp[0][0] = 1;
    for (long long i = 1; i <= k; i++)
    {
        dp[i][0] = 1;
        for (long long j = 1; j <= s; j++)
        {
            dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % mod;
        }
    }
    for (long long i = 1; i <= s; i++)
    {
        dp[k][i] = (dp[k][i - 1] + dp[k][i]) % mod;
    }
    if (n > 0)
    {
        bkt(1, 0);
    }
    ans = (dp[k][s] - k * s - 1 - ans);
    if (ans < 0)
    {
        ans += mod;
    }
    ans %= mod;
    out << ans;
    in.close();
    out.close();
    return 0;
}