Cod sursa(job #2409449)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 19 aprilie 2019 00:28:12
Problema Cowfood Scor 58
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 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[35][10004], mx[35], v[25][35];

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];
    all = (all-((s*k+1)%M)+M)%M;
    for (int t = 1; t < (1<<n); ++t){
        for (int i = 1; i <= k; ++i)
            mx[i] = 0;
        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;
}