Pagini recente » Cod sursa (job #3321291) | Cod sursa (job #895287) | Cod sursa (job #3284256) | Cod sursa (job #146295) | Cod sursa (job #3338459)
#include <bits/stdc++.h>
using namespace std;
static const int MOD = 3210121;
int add(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
int sub(int a, int b) {
a -= b;
if (a < 0) a += MOD;
return a;
}
int mul(long long a, long long b) {
return (int)((a * b) % MOD);
}
int main() {
freopen("cowfood.in", "r", stdin);
freopen("cowfood.out", "w", stdout);
int K, S, N;
cin >> K >> S >> N;
vector<vector<int>> bad(N, vector<int>(K));
for (int i = 0; i < N; i++)
for (int j = 0; j < K; j++)
cin >> bad[i][j];
// Precompute binomials C[n][k], k <= K
int MAXN = S + K;
vector<vector<int>> C(MAXN + 1, vector<int>(K + 1, 0));
for (int n = 0; n <= MAXN; n++) {
C[n][0] = 1;
for (int k = 1; k <= min(n, K); k++)
C[n][k] = add(C[n - 1][k - 1], C[n - 1][k]);
}
auto count_leq_sum = [&](int lim) {
if (lim < 0) return 0;
return C[lim + K][K];
};
// Total mixtures with sum <= S
int total = count_leq_sum(S);
total = sub(total, 1); // zero vector
total = sub(total, mul(K, S)); // exactly one nonzero component
int M = 1 << N;
// mx[mask][j] stored implicitly via dp
vector<vector<int>> mx(M, vector<int>(K, 0));
vector<int> sum(M, 0);
int invalid = 0;
for (int mask = 1; mask < M; mask++) {
int lsb = mask & -mask;
int prev = mask ^ lsb;
int id = __builtin_ctz(lsb);
int s = sum[prev];
for (int j = 0; j < K; j++) {
int v = max(mx[prev][j], bad[id][j]);
mx[mask][j] = v;
s += v - mx[prev][j];
if (s > S) break;
}
sum[mask] = s;
if (s > S) continue;
int ways = count_leq_sum(S - s);
if (__builtin_popcount(mask) & 1)
invalid = add(invalid, ways);
else
invalid = sub(invalid, ways);
}
int answer = sub(total, invalid);
cout << answer << "\n";
return 0;
}