Cod sursa(job #885507)

Utilizator alexalghisiAlghisi Alessandro Paolo alexalghisi Data 22 februarie 2013 08:32:32
Problema Oite Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <iostream>
#include <fstream>
#include <vector>
#define DN 1030
#define KEY 4013
#define pb push_back
#define un unsigned
using namespace std;

vector<int> hash[KEY];
int v[DN];

int caut(int val)
{
    int nivel=(val%KEY),ap=0;
    for(un int i=0;i<hash[nivel].size();++i)
        if(hash[nivel][i]==val)
            ++ap;
    return ap;
}
void insert(int val)
{
    int nivel=(val%KEY);
    hash[nivel].pb(val);
}

int main()
{
    int n,S,rez=0;
    ifstream f("oite.in");
    ofstream g("oite.out");
    f>>n>>S;
    for(int i=1;i<=n;++i)
        f>>v[i];

    for(int i=1;i<=n;++i)
    {
        for(int j=i+1;j<=n;++j)
        {
            if(S-v[i]-v[j]<0)
                continue;
            rez+=caut(S-v[i]-v[j]);
        }
        for(int j=1;j<i;++j)
            insert(v[i]+v[j]);
    }
    g<<rez;
    return 0;
}