Cod sursa(job #2514263)

Utilizator PopescuAndreiAlexandruPopescu Andrei Alexandru PopescuAndreiAlexandru Data 24 decembrie 2019 23:20:30
Problema Oite Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

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

const int MOD = 100021;

vector < pair<int,int> > Hash[MOD];

int n,v[2005],l;

long long int ans=0;

void Push_Hash(int value)
{
    int List=value%MOD;
    vector < pair<int,int> > ::iterator it;
    for(it=Hash[List].begin();it!=Hash[List].end();it++)
    {
        if((*it).first==value)
        {
            (*it).second++;
            return;
        }
    }
    Hash[List].push_back(make_pair(value,1));
}

int Find_Hash(int value)
{
    int List=value%MOD;
    vector < pair<int,int> > ::iterator it;
    for(it=Hash[List].begin();it!=Hash[List].end();it++)
    {
        if((*it).first==value)
            return (*it).second;
    }
    return 0;
}

int main()
{
    fin>>n>>l;
    for(int i=1;i<=n;i++)
        fin>>v[i];
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if (v[i]+v[j]<=l)
                ans+=Find_Hash(l-v[i]-v[j]);
        }
        for(int j=i-1;j>=1;j--)
            Push_Hash(v[i]+v[j]);
    }
    fout<<ans;
    return 0;
}