Cod sursa(job #2338788)

Utilizator triscacezarTrisca Vicol Cezar triscacezar Data 7 februarie 2019 20:19:41
Problema Cowfood Scor 68
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <bits/stdc++.h>
#define Int long long

using namespace std;

ifstream f("cowfood.in");
ofstream g("cowfood.out");

const Int mod=3210121;

int k,s,n,i,j,ans,b[35],a[25][35];
Int fac[20010];

Int put(Int b,int e)
{
    Int ret=1;
    while(e)
    {
        if(e&1)
            ret=(ret*b)%mod;
        b=(b*b)%mod;
        e>>=1;
    }
    return ret;
}

int C(int n,int k)
{
    Int ret=(fac[n]*put(fac[k],mod-2))%mod;
    //cout<<n<<' '<<k<<" : "<<fac[n]<<' '<<fac[k]<<' '<<fac[n-k]<<'\n';
    return (ret*put(fac[n-k],mod-2))%mod;
}

void bkt(int poz,int x,int c[35])
{
//    cout<<poz<<": "<<x<<'\n';
//    for(i=1; i<=k; i++)
//        cout<<c[i]<<' ';
//    cout<<'\n';
//    cout<<'\n';
    if(poz==n+1)
    {
        if(!x)
            return;
        int sum=0;
        for(i=1; i<=k; i++)
            sum+=c[i];
        if(sum>s)
            return;
        if(x&1)
        {
            int aux=C(s-sum+k,k);
            ans=(aux+ans>=mod)?aux+ans-mod:aux+ans;
        }
        else
        {
            int aux=C(s-sum+k,k);
            ans=(ans-aux<0)?ans-aux+mod:ans-aux;
        }
        return ;
    }
    int newc[35];
    for(i=1;i<=k;i++)
        newc[i]=c[i];
    bkt(poz+1,x,c);
    for(i=1; i<=k; i++)
        newc[i]=max(newc[i],a[poz][i]);
    bkt(poz+1,x+1,newc);
}

int main()
{
    f>>k>>s>>n;
    fac[0]=1;
    for(i=1; i<=s+k; i++)
        fac[i]=(fac[i-1]*i)%mod;
    for(i=1; i<=n; i++)
        for(j=1; j<=k; j++)
            f>>a[i][j];
    bkt(1,0,b);
    ans=(C(s+k,k)-ans+mod)%mod;
    ans-=k*s+1;
    g<<(ans%mod+mod)%mod;
    return 0;
}