Cod sursa(job #2533401)

Utilizator Alex_BubBuburuzan Alexandru Alex_Bub Data 28 ianuarie 2020 23:15:11
Problema Diamant Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <fstream>
#include <unordered_map>

using namespace std;

ifstream fin("diamant.in");
ofstream fout("diamant.out");

const int MOD = 10000, NMax = 20;

int N, M, X, Cost[NMax * NMax + 5], ct;
unordered_map <int, int> DP[2];

int main()
{
    fin >> N >> M >> X;

    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= M; j++)
            Cost[++ct] = i * j;

    DP[0][0] = 1;

    for(int code = 1; code <= ct; code++) {
        int line = (code & 1), linePred = ((code - 1) & 1), cost = Cost[code];

        DP[line].clear();

        for(auto cell : DP[linePred]) {
            int sum = cell.first, nr = cell.second;

            DP[line][sum] = (DP[line][sum] + nr) % MOD;
            DP[line][sum - cost] = (DP[line][sum - cost] + nr) % MOD;
            DP[line][sum + cost] = (DP[line][sum + cost] + nr) % MOD;
        }
    }
    fout << DP[ct & 1][X] << '\n';

    fin.close();
    fout.close();

    return 0;
}