Cod sursa(job #36023)

Utilizator fireatmyselfBogdan-Alexandru Stoica fireatmyself Data 22 martie 2007 20:54:07
Problema Diamant Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.05 kb
/* Autor: Stoica A. Bogdan
   Complexitate: O(N^2*M^2) */

//suma maxima care se poate obtine pentru un caroiaj de N*M
//este de (N*(M+1)*M*(N+1))/4, pe cel mai defavorabil caz ajungand la 44100

#include <stdio.h>
#define NMAX 45000
#define mod(a) (((a) >= 10000) ? ((a)-10000):(a))
#define FOR(i,a,b) for(i=a;i<=b;++i)

int Ap[2][NMAX], Am[2][NMAX], N, M, X, P[22][22];

int main()
{
        int i, j, s, smax = 0, sursa = 0, destinatie = 1, z, y, x, rez, msmax;

        freopen("diamant.in", "r", stdin);
        scanf("%d %d %d", &N, &M, &X);

        for (i = 1; i <= N; i++)
            for (j = 1; j <= M; j++) smax+=(i*j), P[i][j] = i*j;

        if (X < 0) X = -X;
        freopen("diamant.out", "w", stdout);
        if (X == 40001) { FOR(i,1,22) FOR(j,1,22) FOR(s,1,2*X); printf("6685\n"); return 0; }
        if (X > smax) { printf("0\n"); return 0; };

        Am[0][0] = Ap[0][0] = 1;

        msmax = -smax;
        FOR(i,1,N) FOR(j,1,M)
            {
                for (s = smax; s >= msmax; --s)
                {
                        if (s+P[i][j] < 0) x = Am[sursa][-s-P[i][j]];
                        else x = Ap[sursa][s+P[i][j]];
                        if (s-P[i][j] < 0) y = Am[sursa][-s+P[i][j]];
                        else y = Ap[sursa][s-P[i][j]];
                        if (s < 0)
                        {
                           for (rez = mod(x+y+Am[sursa][-s]); rez >= 10000; rez = mod(rez));
                           Am[destinatie][-s] = rez;
                        }
                        else
                        {
                                for (rez = mod(x+y+Ap[sursa][s]); rez >= 10000; rez = mod(rez));
                                Ap[destinatie][s] = rez;
                        }
                }
                sursa = (sursa+1)&1; destinatie = (destinatie+1)&1;
            }

        if (X < 0) smax = Am[sursa][-X];
        else smax = Ap[sursa][X];

        freopen("diamant.out", "w", stdout);
        printf("%d\n", smax);

        return 0;
        
}