Cod sursa(job #2845142)

Utilizator valentinchipuc123Valentin Chipuc valentinchipuc123 Data 7 februarie 2022 16:04:29
Problema Cowfood Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.7 kb
#include <iostream>
#include <fstream>
using namespace std;
const int MOD = 3210121,
          NMAX = 21,
          KMAX = 31,
          SMAX = 10001;
int K, S, N, V, E;
int M[NMAX][KMAX], x[NMAX], MAX[NMAX][KMAX], SCOMB[SMAX];
void calcSumComb()
{
    for ( int i = 0; i <= S; i++ )
        SCOMB[i] = 1;

    for ( int i = 1; i <= K; i++ )
        for ( int j = 1; j <= S; j++ )
        {
            SCOMB[j] += SCOMB[j - 1];
            SCOMB[j] %= MOD;
        }
}
void genSubm ()
{
    int k = 1;
    x[1] = 0;

    while ( k > 0 )
    {
        if ( x[k] < N )
        {
            x[k]++;
            int sum = 0;

            for ( int i = 1; i <= K; i++ )
            {
                MAX[k][i] = max ( MAX[k - 1][i], M[x[k]][i] );
                sum += MAX[k][i];
            }

            if ( sum <= S )
            {
                if ( k % 2 != 0 )
                {
                    E += SCOMB[S - sum];
                    E %= MOD;
                }
                else
                {
                    E -= SCOMB[S - sum];

                    if ( E < 0 ) E += MOD;
                }
                k++;
                x[k] = x[k - 1];
            }

        }
        else k--;
    }
}
class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;
	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}
public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}
	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
} fi("cowfood.in");
class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;
    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }
public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }
    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }
    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }
    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
} fo("cowfood.out");
int main()
{
    fi >> K >> S >> N;
    calcSumComb();

    for ( int i = 1; i <= N; i++ )
        for ( int j = 1; j <= K; j++ )
            fi >> M[i][j];
    E = 0;
    genSubm();
    V = SCOMB[S] + ( MOD - S * K - 1 );
    V %= MOD;
    V += MOD - E;
    V %= MOD;
    fo << V;
    return 0;
}