Cod sursa(job #881826)

Utilizator repp4raduRadu-Andrei Szasz repp4radu Data 18 februarie 2013 17:49:56
Problema Diamant Scor 90
Compilator cpp Status done
Runda oni_mixed Marime 1.33 kb
#include <fstream>
#include <vector>
#include <cstring>

#define MAX 41111
#define REST 10000
#define pb push_back

using namespace std;

int N, M, X;
int dp[MAX << 1], aux[MAX << 1];

#define dp (dp + MAX)
#define aux (aux + MAX)

vector<int> V;

void afisare(int val)
{
    ofstream out("diamant.out"); out<<val; out.close();
}

int abs(int X)
{
    if(X < 0) return -X;
    return X;
}

inline int MOD(int X)
{
    if(X >= REST) return X - REST;
    return X;
}

int main()
{
    ifstream in("diamant.in"); in>>N>>M>>X; in.close();
    //daca X > valoarea maxima care se obtine... fuck this shit
    if(abs(X) >= MAX - 2)
    {
        afisare(0);
        return 0;
    }
    //adaug valorile casutelor
    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= M; j++)
            V.pb(i * j);
    dp[0] = aux[0] = 1;
    for(size_t i = 0; i < V.size(); i++)
    {
        //pun +1
        for(int j = -MAX + 3; j < MAX - V[i] - 2; j++)
            if(dp[j])
                aux[j + V[i]] = MOD(aux[j + V[i]] + dp[j]);
        //pun -1
        for(int j = MAX - 3; j > -MAX + V[i] + 2; j--)
            if(dp[j]) aux[j - V[i]] = MOD(aux[j - V[i]] + dp[j]);
        //getReadyForNextPas
        memcpy(dp - MAX, aux - MAX, (MAX << 1)* sizeof(int));
    }
    afisare(dp[X]);
    return 0;
}