Cod sursa(job #474201)

Utilizator Bogdan_CCebere Bogdan Bogdan_C Data 2 august 2010 19:11:11
Problema Oite Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include<stdio.h>
#include<algorithm>

using namespace std;

#define NMAX 1030
#define MOD 666013
#define FIN "oite.in"
#define FOUT "oite.out"

struct hash
{
    int x , y;
    int lana;
    hash* Next;
    } *H[MOD + 20];
 
int C,L,A[NMAX],temp_sum ,REZ = 0,hh;   

void insert(int val , int oita1 , int oita2) 
 {
        int h = val % MOD;
        hash *hsh = new hash;
        hsh -> lana = val;
        hsh -> x = oita1;
        hsh -> y = oita2;
        hsh -> Next = H[h];
        H[h] = hsh;
        }   

void remove(int val)
{
    int h  = val % MOD;
    H[h] = NULL;
    }        
    
int main()
{
    freopen(FIN , "r" , stdin);
    freopen(FOUT , "w" , stdout);
    scanf("%d %d\n",&C , &L);
    for(int i = 0 ; i < C ;++i)
        scanf("%d",&A[i]);
     sort(A , A + C);
     for(int i = 0 ; i < C ; ++i)
      for(int j = i + 1 ; j < C ;++j)
       insert(A[i] + A[j] , i , j);
     for(int i = 0 ; i < C ;++i)
      for(int j = i + 1 ; j < C ;++j)
       {
            temp_sum = L - A[i] - A[j];
            if(temp_sum > 0)
             {
                  hh = temp_sum % MOD;
                  if(H[hh])
                    for( hash* tmp = H[hh] ; tmp ; tmp = tmp ->Next) 
                     if(tmp->lana == temp_sum && i != tmp -> x && i != tmp -> y && j != tmp -> x && j != tmp -> y) 
                         REZ++;  
                    }
            }   
       printf("%d",REZ/6);       
    
    }