Cod sursa(job #1770198)

Utilizator ionanghelinaIonut Anghelina ionanghelina Data 3 octombrie 2016 21:14:16
Problema Oite Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include<bits/stdc++.h>
#define cheie 123457
#define maxC 1035
#define dim 1005
using namespace std;
typedef struct tip
{
  int sum,a,b;
};
char buff[dim+5];
vector<tip> h[cheie+5];
vector<tip>::iterator it;
void hadd(tip x)
{
    int y=x.sum%cheie;
    h[y].push_back(x);
}
long long hsearch(int x,int a,int b)
{
    int y=x%cheie,suma,v1,v2;
    long long sol=0;
    for(it=h[y].begin();it!=h[y].end();it++)
    {
        suma=(*it).sum;
        v1=(*it).a;
        v2=(*it).b;
        if(suma==x)
        {
            long long val=(v1^a)*(v2^a)*(v1^b)*(v2^b);
            if(val) sol=sol+1LL;
        }
    }
    return sol;
}
int poz=0;
int n,l,v[maxC];
long long posibilitati;
int main()
{
    freopen("oite.in","r",stdin);
    freopen("oite.out","w",stdout);
    scanf("%d%d",&n,&l);
    for(int i=1;i<=n;i++)
    {
        //citeste(v[i]);
        scanf("%d",&v[i]);
    }
    sort(v+1,v+n+1);
    for(int i=1;i<=n;i++)
    {
        for(int j=(i+1);j<=n && (v[i]+v[j])<=l;j++)
        {
            int sum=v[i]+v[j];
            tip x;
            x.sum=sum;
            x.a=i;
            x.b=j;
            hadd(x);
        }
    }
    int s1=v[1]+v[2];
    for(int i=1;i<=n && ((v[i]+v[i+1]+s1)<=l);i++)
    {
        for(int j=(i+1);j<=n && (v[i]+v[j]+s1)<=l;j++)
        {
            {
                int dif=l-v[i]-v[j];
                posibilitati=posibilitati+hsearch(dif,i,j);
            }
        }
    }
    posibilitati>>=1;
    posibilitati/=3LL;
    printf("%lld\n",posibilitati);
    return 0;
}