Cod sursa(job #1838716)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 1 ianuarie 2017 17:03:32
Problema Oite Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <iostream>
#include <cstdio>
#include <unordered_map>
#define MAXN 1050
#define MOD 666013

using namespace std;

int c, L;
int a[MAXN];
unsigned int sol;

void read()
{
    scanf("%d %d", &c, &L);
    for (int i = 1; i <= c; i++)
       scanf("%d", &a[i]);
}

struct hnod
{
    int val, amo;
    hnod *next;
    hnod(int x = -1)
    {
        val = x;
        amo = 1;
        next = NULL;
    }
};
hnod *hmap[MOD];

void increment(int pos, hnod *&nod)
{
    if (nod == NULL)
        nod = new hnod(pos);
    else if (nod->val == pos)
        nod->amo++;
    else
        increment(pos, nod->next);
}

void increment(int pos)
{
    increment(pos, hmap[pos % MOD]);
}

int get(int pos)
{
    for (hnod *crt = hmap[pos%MOD]; crt != NULL; crt = crt->next)
        if (crt->val == pos)
            return crt->amo;
    return 0;
}

void solve()
{
    for (int i = 3; i <= c; i++) {
        for (int j = 1; j < i-1; j++)
            increment(a[j] + a[i-1]);
        for (int j = i+1; j <= c; j++)
            sol += get(L-a[i]-a[j]);
    }
}

int main()
{
    freopen("oite.in", "r", stdin);
    freopen("oite.out", "w", stdout);

    read();
    solve();
    printf("%u", sol);

    return 0;
}