Cod sursa(job #2514248)

Utilizator PopescuAndreiAlexandruPopescu Andrei Alexandru PopescuAndreiAlexandru Data 24 decembrie 2019 22:57:12
Problema Oite Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

#define MOD 100021
#define x first
#define y second

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

int n,v[1050],l,ans=0;

void Read()
{
    fin>>n>>l;
    for(int i=1;i<=n;i++)
        fin>>v[i];
}

void Push_Hash(int value)
{
    int List=value%MOD,ok=0;
    for(unsigned ct=0;ct<Hash[List].size();ct++)
    {
        int a=Hash[List][ct].x;
        if(a==value)
        {
            ok=1;
            Hash[List][ct].y++;
            break;
        }
    }
    if(!ok)
        Hash[List].push_back(make_pair(value,1));
}

int Find_Hash(int value)
{
    int List=value%MOD;
    for(unsigned ct=0;ct<Hash[List].size();ct++)
    {
        int a=Hash[List][ct].x;
        int b=Hash[List][ct].y;
        if(a==value)
            return b;
    }
    return 0;
}

void Sol()
{
    for(int i=1;i<n;i++)
    {
        for(int j=i+1;j<=n;j++)
            ans+=Find_Hash(l-(v[i]+v[j]));
        for(int j=1;j<i;j++)
        {
            if((v[i]+v[j])<=l){
            Push_Hash(v[i]+v[j]);}
        }
    }
    fout<<ans<<'\n';
}

int main()
{
    Read();
    Sol();
}