Cod sursa(job #2810036)

Utilizator grecu_tudorGrecu Tudor grecu_tudor Data 28 noiembrie 2021 12:06:46
Problema Cowfood Scor 54
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

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

const int MAXS = 10000;
const int MAXK = 30;
const int MAXN = 20;
const int MOD = 3210121;

int cnt[MAXN + 5][MAXK + 5];
int comb[MAXS + MAXK + 5][MAXK + 5];
int a[MAXK + 5][MAXN + 5];

void precalc() {
  for ( int i = 1; i <= MAXS + MAXK; ++i ) {
	comb[i][1] = i;
    for ( int j = 2; j <= MAXK; ++j ) {
      comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % MOD;
    }
  }
}

int sum[MAXN + 5];
int sp, h, bad;

int N, K, S;

void pinex( int pos ) {
  if ( pos == N ) {
	if ( h == 0 ) return;
	sp = 0;
	for ( int i = 0; i < K; ++i ) {
	  sp += a[i][pos - 1];
	}
	if ( h & 1 ) bad = (bad + sum[S - sp]) % MOD;
	else bad = (bad - sum[S - sp]) % MOD;
	return;
  }
  if ( pos ) {
    for ( int i = 0; i < K; ++i ) {
      a[i][pos] = a[i][pos - 1];
    }
  }
  pinex( pos + 1 );
  ++h;
  for ( int i = 0; i < K; ++i ) {
	if ( pos ) {
	  a[i][pos] = max( a[i][pos - 1], cnt[pos][i] );
    } else {
	  a[i][pos] = cnt[pos][i];
	}
  }
  pinex( pos + 1 );
  --h;
}

int main() {
  int k, s, n;

  fin >> k >> s >> n;
  for ( int i = 0; i < n; ++i ) {
	for ( int j = 0; j < k; ++j ) {
	  fin >> cnt[i][j];
	}
  }
  precalc();
  sum[0] = 1;
  for ( int i = 1; i <= s; ++i ) {
    sum[i] = (sum[i-1] + comb[i + k - 1][k - 1]) % MOD;
  }
  N = n, K = k, S = s;
  for ( int i = 0; i < K; ++i ) {
	a[i][0] = 0;
  }
  pinex( 0 );
  bad = (bad + 1 + s * k) % MOD;
  fout << (sum[s] - bad + MOD) % MOD;
  fin.close();
  fout.close();
  return 0;
}