Cod sursa(job #2409442)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 19 aprilie 2019 00:17:54
Problema Cowfood Scor 18
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <bits/stdc++.h>
#define ff first
#define ss second

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;

const string file = "cowfood";
const ll INF = 9223372036854775807ll;
const int inf = 2147483647, M = 3210121;

int k, s, n, pd[31][10004], mx[31], v[21][31];

int main()
{
    ifstream fin (file+".in");
    ofstream fout (file+".out");
    fin >> k >> s >> n;
    for (int i = 0; i < n; ++i)
        for (int j = 1; j <= k; ++j)
            fin >> v[i][j];
    for (int i = 0; i <= k; ++i)
        pd[i][0] = 1;
    for (int j = 0; j <= s; ++j)
        pd[0][j] = 1;
    for (int i = 1; i <= k; ++i)
        for (int j = 1; j <= s; ++j)
            pd[i][j] = (pd[i-1][j] + pd[i][j-1])%M;
    int all = pd[k][s-k];
    for (int t = 1; t < (1<<n); ++t){
        memset(mx, 0, sizeof(mx));
        int mult = 1;
        for (int i = 0; i < n; ++i)
            if(t&(1<<i)){
                mult *= -1;
                for (int j = 1; j <= k; ++j)
                    mx[j] = max(mx[j], v[i][j]);
            }
        int now = s;
        for (int i = 1; i <= k; ++i)
            now -= mx[i];
        if(now < 0)
            continue;
        all = (all+pd[k][now]*mult+M)%M;
    }
    fout << all << "\n";
    return 0;
}