Cod sursa(job #3298776)

Utilizator anca.gdDumitru Anca Gabriela anca.gd Data 1 iunie 2025 16:29:58
Problema Diamant Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.08 kb
#include <fstream>
using namespace std;
int valori[403], negativ[50000],pozitiv[50000],  vfneg[50000],vfpoz[50000];
int n,m,X;
int mod=10000;
int main()
{
    ifstream fin("diamant.in");
    ofstream fout("diamant.out");
    fin>>n>>m>>X;
    if(X>44100) {
        fout<<0;
        return 0;
    }
    if(X<-44100) {
        fout<<0;
        return 0;
    }
    int indx=0;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m;j++)
            valori[++indx]=i*j;
    negativ[1]=1; pozitiv[1]=1;
    for (int k=2; k<=indx; k++){
        //+1
        for(int i=44100; i>0; i--)
            if(pozitiv[i]){
                pozitiv[i+valori[k]]+=pozitiv[i];
                pozitiv[i+valori[k]]%=mod;
                vfpoz[i+valori[k]]=1;
            }
        for(int i=1; i<=44100; i++)
            if(negativ[i]){
                int where=-i+valori[k];
                if(where>=0){
                    pozitiv[where]+=negativ[i];
                    pozitiv[where]%=mod;
                    vfpoz[where]=1;
                }
                else{
                    negativ[-where]+=negativ[i];
                    negativ[-where]%=mod;
                    vfneg[-where]=1;
                }
            }
        //-1
        for (int i=44100; i>0; i--)
            if(negativ[i]&&!vfneg[i]){
                int where=i+valori[k];
                negativ[where]+=negativ[i];
                negativ[where]%=mod;
            }
        for(int i=1; i<=44100; i++)
            if(pozitiv[i]&&!vfpoz[i]){
                int where=i-valori[k];
                if(where>=0)
                    pozitiv[where]+=pozitiv[i], pozitiv[where]%=mod;
                else
                    negativ[-where]+=pozitiv[i], negativ[-where]%=mod;
            }
        for (int i=0; i<=44100; i++)
            { vfpoz[i]=0; vfneg[i]=0;}
        pozitiv[valori[k]]+=pozitiv[0];
        negativ[valori[k]]+=pozitiv[0];
        if(!pozitiv[0]) pozitiv[valori[k]]++, negativ[valori[k]]++;
    }
    if(X<0) fout<<negativ[-X];
        else fout<<pozitiv[X];
    return 0;
}