Pagini recente » Cod sursa (job #1672322) | Cod sursa (job #2838219) | Cod sursa (job #1411795) | Cod sursa (job #3146530) | Cod sursa (job #2809305)
#include <fstream>
using namespace std;
ifstream fin ("cowfood.in");
ofstream fout ("cowfood.out");
int const N = 2e4 + 7 , mod = 3210121;
int k , s , n , v [21][31], now [31] , dp [N][31];
void add (int &x , int y){
x = (x + y) % mod;
}
void precalc (){
dp [0][0] = 1;
for(int i = 0 ; i < N - 1 ; ++ i)
for(int j = 0 ; j <= min (k , i) ; ++ j){
if (i)
add (dp [i][j] , dp [i - 1][j]);
if (i && j)
add (dp [i][j] , dp [i - 1][j - 1]);
}
}
void read (){
fin >> k >> s >> n;
for(int i = 0 ; i < n ; ++ i)
for(int j = 0 ; j < k ; ++ j)
fin >> v [i][j];
}
int main()
{
read ();
precalc ();
int total (dp [s][k]);
for(int i = 1 ; i < (1 << n) ; ++ i){
int sum (0) , c (0);
fill (now , now + k , 0);
for(int j = 0 ; (1 << j) <= i ; ++ j)
if ((1 << j) & i){
++ c;
for(int o = 0 ; o < k ; ++ o){
sum -= now [o];
now [o] = max (now [o] , v [j][o]);
sum += now [o];
}
}
if (sum > s)
continue;
total = total + (c % 2) * -1 * dp [s - sum + k][k];
if (total < 0)
total += mod;
if (total >= mod)
total -= mod;
}
fout << total << '\n';
fin.close ();
fout.close ();
return 0;
}