Pagini recente » Cod sursa (job #347420) | Cod sursa (job #3253282) | Cod sursa (job #2503161) | Cod sursa (job #400682) | Cod sursa (job #2408452)
#include<bits/stdc++.h>
#define mod 3210121
using namespace std;
ifstream f("cowfood.in");
ofstream g("cowfood.out");
int k, s, n;
short nr[35][35];
int combi[10035][32];
bool xo[(1<<20)];
void comb()
{
combi[0][0] = combi[1][0] = combi[1][1] = 1;
for(int i = 2; i <= 10033; ++i)
{
combi[i][0] = 1;
for(int j = 1; j <= 30; ++j)
{
combi[i][j] = (combi[i-1][j] + combi[i-1][j-1]);
if(combi[i][j] >= mod)
combi[i][j] -= mod;
}
}
for(int i = 0; i <= k; ++i)
for(int j = i; j <= 10033; ++j)
{
combi[j][i] += combi[j-1][i];
if(combi[j][i] >= mod)
combi[j][i] -= mod;
}
}
short mx[(1<<20)][30];
int main()
{
f >> k >> s >> n;
for(int i = 0; i < n; ++i)
for(int j = 0; j < k; ++j)
f >> nr[i][j];
comb();
int ans = 0;
for(int j = s; j >= 2; --j)
{
ans = ans + (combi[j + k - 1][k - 1] - combi[j + k - 2][k - 1]) - k + mod;
while(ans >= mod)
ans -= mod;
}
for(int i = 1; i < (1<<n); ++i)
{
bool bb = 0;
int ss = 0;
for(int j = 0; j < n; ++j)
if(i & (1<<j))
{
bb = xo[i - (1<<j)] ^ 1;
for(int p = 0; p < k; ++p)
mx[i][p] = max(mx[i - (1<<j)][p], nr[j][p]), ss += mx[i][p];
break;
}
xo[i] = bb;
if(ss > s)
continue;
if(bb)
{
ans -= combi[s - ss + k - 1][k - 1];
if(ans < 0)
ans += mod;
}
else
{
ans += combi[s - ss + k - 1][k - 1];
if(ans >= mod)
ans -= mod;
}
}
g << ans;
return 0;
}