Cod sursa(job #53850)

Utilizator mariusdrgdragus marius mariusdrg Data 23 aprilie 2007 15:36:17
Problema Oite Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.55 kb
#include<stdio.h>
#include<vector>

using namespace std;

const int maxn = 1025;
const int tabm = 666013;
int i;
int n;
int k;
int j;
int a[maxn];
long long sol;
vector<int> vect[tabm];
vector<int> vect2[tabm];
int ta[tabm];
int ta2[tabm];

int hash(int i)
{
        int h = (i << 4) ^ (i >> 28);
        if (h < 0) h *= -1;
        h %= tabm;
        return h;
}

int main()
{
        freopen("oite.in","r",stdin);
        freopen("oite.out","w",stdout);
        scanf("%d %d",&n,&k);
        for(i = 1;i <= n; ++i)
        {
                scanf("%d",&a[i]);
        }
        for(i = 1;i <= n; ++i)
        {
                for(j = 1;j < i; ++j)
                {
                        if (a[i] + a[j] <= k)
                        {
                       //         int aux = sol;
                                int nrsol = 0;
                                int k1 = 0;
                                for(k1 = 0; k1 < ta[hash(k - a[i] - a[j])]; ++k1)
                                {
                                        if (vect[hash(k - a[i] - a[j])][k1] == k - a[i] - a[j]) nrsol++;
                                }
                                sol += (long long)nrsol;
              //                  if (ta[hash(k - a[i] - a[j])] > 0)
                 /*               {
                                        printf("%d\n",k - a[i] - a[j]);
                                }*/
                                nrsol = 0;

                                if (a[i] + a[j] * 2 <= k)
                                {
                                        for(k1 = 0; k1 < ta2[hash(k - a[i] - 2 * a[j])]; ++k1)
                                        {
                                                if (vect2[hash(k - a[i] - 2 * a[j])][k1] == k - a[i] - 2 * a[j]) nrsol++;
                                        }
                                        sol -= nrsol;
                                        if (k - a[i] - a[j] * 2 == a[j]) (long long)sol++;
                                 }
                                //if (sol < 0) printf("%d %d\n",i,j);
                    //            if (sol > aux) printf("%d %d %d\n",i,j,sol - aux);
                        }
                }
           //     printf("%d %d\n",i,sol);
                for(j = 1;j < i; ++j)
                {
                        ta[hash(a[i] + a[j])]++;
                        vect[hash(a[i] + a[j])].push_back(a[i] + a[j]);
                }
                ta2[hash(a[i])]++;
                vect2[hash(a[i])].push_back(a[i]);
        }
        printf("%lld\n",sol / 3);
        return 0;
}