Cod sursa(job #1742372)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 16 august 2016 13:01:50
Problema Oite Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstring>

#define BYTE 0xff+1
#define getByte(X,Y) (*(((unsigned char *)&X)+Y))
#define NR_BYTES (sizeof(uint32_t))
#define MAX_N 10000000 + 1

using namespace std;

ifstream f ("oite.in");
ofstream t ("oite.out");

uint32_t c,l,s=0;
const int key=47,mod=1049000;

uint32_t v[1024];
int32_t hsh[mod+1];

void increment(int32_t &i)
{
    if (++i==mod) i=0;
}

int32_t hashsum(int32_t x)
{
    return (1LL*x*key+x/key)%mod;
}

void hash_add(int32_t x)
{
    int32_t i=hashsum(x);
    for (; hsh[i]!=0; increment(i));
    hsh[i]=x;
}

int32_t hash_find(int32_t x)
{
    int32_t cnt=0,i=hashsum(x);
    for (; hsh[i]!=0; increment(i))
        if (hsh[i]==x) ++cnt;
    return cnt;
}

void spoof()
{
    for (uint32_t k=2; k<c-1; ++k)
    {
        for (uint32_t y=k+1; y<c; ++y)
        {
            for (uint32_t i=0; i<k-1; ++i)
            {
                for (uint32_t j=i+1; j<k; ++j)
                    hash_add(v[i]+v[j]);
            }
            s+=hash_find(l-v[k]-v[y]);
        }
    }
}

void countsort(uint32_t v1[],uint32_t v2[],uint32_t i)
{
    uint32_t counter[BYTE], index[BYTE];
    memset(counter, 0, sizeof(counter));
    for(uint32_t j = 0; j < c; ++j) ++counter[ getByte(v1[j],i) ];
    index[0] = 0;
    for(uint32_t j = 1; j < BYTE; ++j) index[j] = index[j - 1] + counter[j - 1];
    for(uint32_t j = 0; j < c; ++j)
        v2[ index[ getByte(v1[j],i) ]++ ] = v1[j];
}

void magic()
{
    uint32_t *temp = new uint32_t[c];
    for (uint32_t i=0; i<NR_BYTES; ++i)
        if (i%2==0)
            countsort(v,temp,i);
        else
            countsort (temp,v,i);
}

int main()
{
    f>>c>>l;
    for (uint16_t i=0; i<c; ++i)
        f>>v[i];
    magic();
    spoof();
    t<<s;
    return 0;
}