Cod sursa(job #2150899)

Utilizator MiricaMateiMirica Matei MiricaMatei Data 3 martie 2018 21:14:44
Problema Cowfood Scor 42
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 20;
const int MAX_K = 30;
const int MAX_S = 10000;
const int MOD = 3210121;

int c[MAX_S + MAX_K + 5][MAX_K + 5];
int a[MAX_N + 5][MAX_K + 5];
int b[MAX_K + 5];

int k, s, n;
int ans = 0;

void pinex(int p, int nr, int sum) {
  if (p == n + 1) {
    sum = s - sum;
    if (sum >= 0 && nr != 0) {
      int aux = c[sum + k][k];
      if (nr % 2 == 1)
        ans += aux;
      else
        ans -= aux;
    }
  } else {
    int t[k + 5];
    for (int i = 1; i <= k; ++i)
      t[i] = b[i];
    pinex(p + 1, nr, sum);
    sum = 0;
    for (int i = 1; i <= k; ++i) {
      b[i] = max(b[i], a[p][i]);
      sum += b[i];
    }
    pinex(p + 1, nr + 1, sum);
    for (int i = 1; i <= k; ++i)
      b[i] = t[i];
  }
}

int main() {
  freopen("cowfood.in", "r", stdin);
  freopen("cowfood.out", "w", stdout);

  scanf("%d%d%d", &k, &s, &n);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= k; ++j)
      scanf("%d", &a[i][j]);

  for (int i = 0; i <= s + k; ++i) {
    c[i][0] = 1;
    if (i <= k)
      c[i][i] = 1;
    for (int j = 1; j < i && j <= k; ++j) {
      c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
      if (c[i][j] >= MOD)
        c[i][j] -= MOD;
    }
  }

  pinex(1, 0, 0);

  int aux = (k * s + 1) % MOD;
  aux = c[s + k][k] - aux;
  if (aux < 0)
    aux += MOD;
  aux -= ans;
  if (aux < 0)
    aux += MOD;
  printf("%d\n", aux);

  return 0;
}