Cod sursa(job #1765660)

Utilizator ionutpop118Pop Ioan Cristian ionutpop118 Data 26 septembrie 2016 21:37:14
Problema Oite Scor 90
Compilator cpp Status done
Runda cerculdeinfo-lectia1-hashuri.rabinkarp Marime 1.52 kb
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int mod = 666019;

struct suma
{
    int x, a;
};
vector <suma> g[mod];
int v[1050];


inline void update (int x, int a)
{
    suma m; m.x = x; m.a = a;
    g[x % mod].insert(g[x % mod].end(), m);
}
inline int query (int x, int b)
{
    int poz = x % mod, ans = 0;
    for (int i = 0; i < g[poz].size(); ++i)
        if (g[poz][i].x == x && g[poz][i].a > b)
            ++ans;
    return ans;
}
int main()
{
    freopen("oite.in", "r", stdin);
    freopen("oite.out", "w", stdout);
    int n, k, x, poz, ans;
    long long anss = 0;
    suma m;

    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &v[i]);

    sort (v + 1, v + n + 1);

    for (int i = 1; i < n; ++i)
        for (int j = i + 1; j <= n; ++j)
        {
            //update(v[i] + v[j], i);
            x = v[i] + v[j];
            m.x = x; m.a = i;
            g[x % mod].insert(g[x % mod].end(), m);
        }


    for (int i = 1; i < n; ++i)
        for (int j = i + 1; j <= n; ++j)
            if (v[i] + v[j] <= k)
            {
                x = k - (v[i] + v[j]); poz = x % mod; ans = 0;
                for (int ii = 0; ii < g[poz].size(); ++ii)
                    if (g[poz][ii].x == x && g[poz][ii].a > j)
                        ++ans;
                //anss += (1LL) * query(k - v[i] - v[j], j);
                anss += (1LL) * ans;
            }

    printf("%lld\n", anss);

    return 0;
}