Cod sursa(job #1975497)

Utilizator KusikaPasa Corneliu Kusika Data 1 mai 2017 09:09:23
Problema Cowfood Scor 14
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <bits/stdc++.h>
using namespace std;

//Constants
const long long INF = std::numeric_limits<long long>::max();
const long long MOD = 3210121;
const long double PI = 3.141592653589793;

//MACROS
#define pb push_back
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
typedef long long LL;
typedef pair <int,int> PII;

int n, s, k, a[33][33], cur[33];
LL dp[33][11111], ans;

LL T(LL x) {
    if (x < 0) return x + MOD;
    if (x >= MOD) return x - MOD;
    return x;
}

void brute(int lvl, int mask) {
    if (lvl == n + 1) {
        int ss = 0;
        rep(i,1,k+1) ss += cur[i];
        if (ss >= s) return;
        if (ss == 0) ss = k;
        if (__builtin_popcount(mask)&1) ans = T(ans - dp[k][s-ss]);
        else ans = T(ans + dp[k][s-ss]);
    } else {
        brute(lvl+1,mask);
        int prev[33];
        rep(i,1,k+1) prev[i] = cur[i], cur[i] = max(cur[i],a[lvl][i]);
        brute(lvl+1,mask|(1<<(lvl-1)));
        rep(i,1,k+1) cur[i] = prev[i];
    }
}

int main() {
	freopen("cowfood.in","r",stdin);
	freopen("cowfood.out","w",stdout);
	cin >> k >> s >> n;
	rep(i,1,n+1) rep(j,1,k+1) cin >> a[i][j];
	rep(i,0,11011) dp[0][i] = 1;
	rep(i,1,k+1)
        for (int j = dp[i][0] = 1; j < 11011; j++)
            dp[i][j] = T(dp[i-1][j] + dp[i][j-1]);
	brute(1,0);
	cout << ans;
}