Cod sursa(job #1479393)

Utilizator dnprxDan Pracsiu dnprx Data 31 august 2015 11:18:54
Problema Oite Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <bits/stdc++.h>
#define nmax 1060900
#define P 1060883

using namespace std;

int n, S, a[1030], cnt;
struct pereche
{
    int suma, poz;
};
vector <pereche> h[P];

void Citire()
{
    int i;
    ifstream fin("oite.in");
    fin >> n >> S;
    for (i = 1; i <= n; i++)
        fin >> a[i];
    fin.close();
}

inline void Inserare(int x, int p)
{
    pereche w;
    w.suma = x;
    w.poz = p;
    int k = x % P;
    h[k].push_back(w);
}

void TabelaHash()
{
    int i, j, x;
    for (i = 1; i < n; i++)
        for (j = i + 1; j <= n; j++)
        {
            x = a[i] + a[j];
            Inserare(x, i);
        }
}

void Rezolva()
{
    int i, j, suma, k;
    pereche w;
    vector <pereche>::iterator it;
    for(i = 1; i < n; i++)
        for(j = i + 1; j <= n; j++)
        {
            suma = S - a[i] - a[j];
            if(suma >= 0)
            {
                k = suma % P;
                for(it = h[k].begin(); it != h[k].end(); it++)
                {
                    w = *it;
                    if(w.suma == suma && w.poz > j)
                        cnt++;
                }
            }
        }
}

void Afisare()
{
    ofstream fout("oite.out");
    fout << cnt << "\n";
    fout.close();
}

int main()
{
    Citire();
    TabelaHash();
    Rezolva();
    Afisare();
    return 0;
}