Cod sursa(job #240203)

Utilizator Mishu91Andrei Misarca Mishu91 Data 6 ianuarie 2009 23:30:54
Problema Oite Scor 30
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

typedef struct nod
{
    short i, j;
    nod* next;
}*oaie;

int N, L, V[MAX_N];
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 = new nod;
    w -> i = i, w -> j = j, w -> next = H[k];
    H[k] = w;
}

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

    for(oaie p = H[k]; p; p = p -> next)
        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();
}