Cod sursa(job #2408440)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 17 aprilie 2019 22:53:48
Problema Cowfood Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.92 kb
#include<fstream>
#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];
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;
        bool np = 0;
        for(int j = 0; j < n; ++j)
            if(i & (1<<j))
            {
                bb ^= 1;
                if(!np)
                {
                    for(int p = 0; p < k; ++p)
                        mx[i][p] = max(mx[i - (1<<j)][p], nr[j][p]);
                    np = 1;
                }
            }
        int ss = 0;
        for(int p = 0; p < k; ++p)
            ss += mx[i][p];
        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;
}