Cod sursa(job #53866)

Utilizator mariusdrgdragus marius mariusdrg Data 23 aprilie 2007 16:18:59
Problema Oite Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.18 kb
#include<stdio.h>
#include<set>
#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< pair <int , int> > vect[tabm];
vector< pair <int , int> > vect2[tabm];
int ta[tabm];
int ta2[tabm];

int hash(int i)
{
        int h = 0;
        h ^= (h << 5) + (h >> 2) + i;
        h %= tabm;
        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;
                                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]) . first == k - a[i] - a[j])
                                        {
                                                nrsol = vect[hash(k - a[i] - a[j])][k1] .second;
                                                break;
                                        }
                                }
                                sol += (long long)nrsol;
                                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]) . first == k - a[i] - 2 * a[j])
                                                {
                                                        nrsol = vect2[hash(k - a[i] - 2 * a[j])][k1] . second;
                                                }
                                        }
                                        sol -= nrsol;
                                        if (k - a[i] - a[j] * 2 == a[j])
                                        {
                                                (long long)sol++;
                                        }
                                 }
                          //       if (sol != aux)printf("%d %d %d\n",i,j,sol - aux);
                
                        }
                }
                int ver = 0;
                int aux = k;
                for(j = 1;j < i; ++j)
                {
                        ver = 0;
                         for(k = 0;k < ta[hash(a[i] + a[j])]; ++k)
                         {
                                if (vect[hash(a[i] + a[j])][k] . first == a[i] + a[j])
                                {
                                        ver = 1;
                                        (vect[hash(a[i] + a[j])][k] . second)++;
                                        break;
                                }

                         }
                      //   if (i == 8 && j == 3) printf("\n\n%d\n\n",ver);
                         if (ver == 0)
                         {
                               vect[hash(a[i] + a[j])].push_back(make_pair(a[i] + a[j] , 1));
                        //       if (a[i] + a[j] == 24) printf("\n\n%d %d\n\n",i,j);
                               ta[hash(a[i] + a[j])]++;
                         }
                        
                         
                }
                ver = 0;
                for(k = 0;k < ta2[hash(a[i])]; ++k)
                {
                     if (vect2[hash(a[i])][k] . first == a[i])
                     {
                             ver = 1;
                             (vect2[hash(a[i])][k] . second)++;
                              break;
                     }

               }
               if (ver == 0)
               {
                        ta2[hash(a[i])]++;
                        vect2[hash(a[i])].push_back(make_pair(a[i],1));
               }
               k = aux;
        }
        printf("%lld\n",sol / 3);
        return 0;
}