Cod sursa(job #240206)

Utilizator Mishu91Andrei Misarca Mishu91 Data 6 ianuarie 2009 23:38:18
Problema Oite Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <cstdio>
#include <vector>

using namespace std;

#define MOD 11257
#define MAX_N 1025

struct oaie
{
    short i, j;
}//_attribute_((packed));
;

int N, L, V[MAX_N];
vector <oaie> H[MOD];
long Rez;

void hash(int i, int j)
{
    int x = V[i] + V[j];
    if(x > L) return;
    int k = x % MOD;

    oaie w;
    w.i = i, w.j = j;
    H[k].push_back(w);
}

int search(short i, short j)
{
    int x = L - V[i] - V[j];
    int k = x % MOD;
    int Sol = 0;

    for(vector<oaie>::iterator p = H[k].begin(); p != H[k].end(); ++p)
        if(V[p->i] + V[p->j] == x)
            if(p -> i > j && p -> j != j)
                ++Sol;
    return Sol;
}

void citire()
{
    scanf("%d %d",&N, &L);

    for(int i = 1; i <= N; ++i)
        scanf("%d",V+i);
}

void solve()
{
    for(int i = 1; i < N; ++i)
        for(int j = i+1; j <= N; ++j)
            hash(i, j);

    for(int i = 1; i < N; ++i)
        for(int j = i+1; j <= N; ++j)
            Rez += search(i, j);
    printf("%ld\n",Rez);
}

int main()
{
    freopen("oite.in","rt",stdin);
    freopen("oite.out","wt",stdout);

    citire();
    solve();
}