Cod sursa(job #2514262)

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

using namespace std;

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

const int dim = 1030;
const int MOD = 100021;

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

int n,a[dim],suma;

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()
{
    in >> n >> suma;
    for (int i=1; i<=n; i++)
    {
        in >> a[i];
    }
    for (int i=1; i<=n; i++)
    {
        for (int j=i+1; j<=n; j++)
        {
            if (a[i] + a[j] <= suma)
            {
                ans += Find_Hash(suma-a[i]-a[j]);
            }
        }
        for (int j=i-1; j>=1; j--)
        {
            Push_Hash(a[i]+a[j]);
        }
    }
    out << ans;
    return 0;
}