Cod sursa(job #53840)

Utilizator mariusdrgdragus marius mariusdrg Data 23 aprilie 2007 15:26:39
Problema Oite Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include<stdio.h>


const int maxn = 1025;
const int tabm = 1148576;
int i;
int n;
int k;
int j;
int a[maxn];
long long sol;

int ta[tabm];
int ta2[tabm];

int hash(int i)
{
        return i % tabm;
}

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;
                                sol += ta[hash(k - a[i] - a[j])];
              //                  if (ta[hash(k - a[i] - a[j])] > 0)
                 /*               {
                                        printf("%d\n",k - a[i] - a[j]);
                                }*/
                                if (a[i] + a[j] * 2 <= k)sol -= ta2[hash(k - a[i] - a[j] * 2)];
                                if (k - a[i] - a[j] * 2 == a[j]) 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])]++;
                }
                ta2[hash(a[i])]++;
        }
        printf("%lld\n",sol / 3);
        return 0;
}