Cod sursa(job #3368378)

Utilizator andreidumitrache1709andreidumitrache andreidumitrache1709 Data 14 octombrie 2026 12:52:37
Problema Cowfood Scor 8
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <bits/stdc++.h>
using namespace std;

const int MAXK = 30;
const int MAXN = 20;
const int MAXS = 1e4;
const int MOD = 3210121;

int s , k , n , fact[2 * MAXS + 1] , inv[2 * MAXS + 1] , mat[MAXN + 1][MAXK + 1] , v[MAXK + 1] , cop[MAXK + 1];
bool viz[MAXN + 1];
int binpow( int b , int e ) {
    int ans = 1;
    while( e ) {
        if( e & 1 )
            ans = 1ll * ans * b % MOD;
        b = 1ll * b * b % MOD;
        e >>= 1;
    }
    return ans;
}
void init() {
    int i;
    fact[0] = 1;
    for( i = 1 ; i <= 2 * MAXS ; i++ )
        fact[i] = 1ll * fact[i - 1] * i % MOD;
    inv[2 * MAXS] = binpow( fact[2 * MAXS] , MOD - 2 );
    for( i = 2 * MAXS - 1 ; i >= 0 ; i-- )
        inv[i] = 1ll * inv[i + 1] * ( i + 1 ) % MOD;
}
int comb( int n , int k ) {
    if( n < 0 || k > n )
        return 0;
    return 1ll * fact[n] * inv[k] % MOD * inv[n - k] % MOD;
}
int ans;
void bkt( int pos , int cnt ) {
    int i , j , sum = 0;
    if( cnt % 2 == 0 && cnt != 0 ) {
        for( j = 1 ; j <= k ; j++ )
            sum += v[j];
        sum = s - sum;
        if( sum > 0 ) {
            ans += comb( sum + k , k );
            if( ans >= MOD )
                ans -= MOD;
        }
    } else if( cnt % 2 == 1 ) {
        for( j = 1 ; j <= k ; j++ ) {
           sum += v[j];
        }
        sum = s - sum;
        if( sum > 0 ) {
            ans -= comb( sum + k , k );
            if( ans < 0 )
                ans += MOD;
        }
    }
    if( pos == n )
        return;
    for( j = 1 ; j <= k ; j++ )
        cop[j] = v[j];
    for( j = 1 ; j <= k ; j++ )
        v[j] = max( v[j] , mat[pos + 1][j] );
    bkt( pos + 1 , cnt + 1 );
    for( j = 1 ; j <= k ; j++ )
        v[j] = cop[j];
    bkt( pos + 1 , cnt );
}
int main() {
    ifstream cin( "cowfood.in" );
    ofstream cout( "cowfood.out" );
    int i , l , c , sum , z , j;
    cin >> k >> s >> n;
    for( l = 1 ; l <= n ; l++ )
        for( c = 1 ; c <= k ; c++ )
            cin >> mat[l][c];
    // fac pinnex
    init();
    ans = comb( s , k );
    bkt( 0 , 0 );
    cout << ans << '\n';
    return 0;
}