Pagini recente » Cod sursa (job #3174283) | Cod sursa (job #759487) | Cod sursa (job #2693626) | Cod sursa (job #781124) | Cod sursa (job #1112560)
#include <cstdio>
#include <algorithm>
using namespace std;
const int NMAX = 25, KMAX = 35, SMAX = 10010, MOD = 3210121;
int N, K, S, Ans, Dp[KMAX][SMAX], A[NMAX][KMAX], V[KMAX];
void Go(int Pos, int V[KMAX], int Sgn)
{
if(Pos == N)
{
int Rem = S;
for(int i = 0; i < K; ++ i) Rem -= V[i];
if(Rem >= 0) Ans = (Ans + Sgn * Dp[K][Rem] + MOD) % MOD;
return ;
}
Go(Pos + 1, V, Sgn);
int NextV[KMAX];
for(int i = 0; i < K; ++ i)
NextV[i] = max(V[i], A[Pos][i]);
Go(Pos + 1, NextV, -Sgn);
}
int main()
{
freopen("cowfood.in", "r", stdin);
freopen("cowfood.out", "w", stdout);
scanf("%i %i %i", &K, &S, &N);
for(int i = 0; i < N; ++ i)
for(int j = 0; j < K; ++ j)
scanf("%i", &A[i][j]);
for(int i = 0; i <= S; ++ i) Dp[0][i] = 1;
for(int i = 1; i <= K; ++ i)
for(int j = 0; j <= S; ++ j)
Dp[i][j] = (Dp[i][j - 1] + Dp[i - 1][j]) % MOD;
Go(0, V, 1);
Ans = (Ans - K * S - 1 + MOD) % MOD;
printf("%i\n", Ans);
}