Cod sursa(job #244434)

Utilizator mika17Mihai Alex Ionescu mika17 Data 15 ianuarie 2009 02:24:10
Problema Oite Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

const int NMAX = 1024, HSIZE = 50000;
const double knt = 0.61803398;

int N,S,c[NMAX];

struct node {int fi,se,th;} hash[HSIZE][30];

node make_pair(int a,int b,int c) {
        node tmp = {a,b,c};
        return tmp;
}

inline int h(int x) {

        return int( (double(x * knt) - int(x * knt)) * (HSIZE-1) );
}

int search(int x,int ind)
{
        int hh = h(x);

        for(int i = 1; i <= hash[hh][0] . fi; ++i)
          if(hash[hh][i] . fi == x && hash[hh][i] . th == ind) return i;

        return -1;
}

void insert(int x,int ind) {

        int hh = h(x), t = search(x,ind);
        if(t == -1)
          hash[hh][ ++ hash[hh][0] . fi ] = make_pair(x,1,ind);
         else hash[hh][t] . se ++;
}

int count(int x,int ind) {

        if(x<0) return 0;

        int cnt = 0,hh = h(x);
        
        for(int i = 1; i <= hash[hh][0] . fi; ++i)
          if(hash[hh][i] . fi == x && hash[hh][i] . th > ind)
           cnt += hash[hh][i] . se;

        return cnt;
}

void debug()
{
 for(int i=0;i < HSIZE; ++i)
 {
  for(int j=1; j <= hash[i][0] . fi; ++j)
   printf("(%d %d %d) ",hash[i][j]. fi, hash[i][j] . se, hash[i][j] . th);
  printf("\n");
 }
}


int main() {

        freopen("oite.in","r",stdin);
        freopen("oite.out","w",stdout);

        scanf("%d%d",&N,&S);
        for(int i=1;i<=N;++i)
         scanf("%d",&c[i]);

        for(int i=1;i<N;++i)
        {
         for(int j=i+1;j<=N;++j)
          insert(c[i]+c[j],i);
        }
        //debug();
        int cnt = 0;
        for(int i=1;i<N;++i)
         for(int j=i+1;j<=N;++j)
          cnt += count(S - c[i] - c[j],j);

        printf("%d",cnt);

        return 0;
}