Cod sursa(job #1581901)

Utilizator pepsiM4A1Ozturk Arif pepsiM4A1 Data 27 ianuarie 2016 12:16:57
Problema Oite Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <cstdio>
#include <vector>
#define CMAX 1024
#define MOD 666013
using namespace std;
struct str
{
    int o1;
    int o2;
    int cost;
};
vector<str>h[MOD];
int c,l;
int v[CMAX];
void addtohash(str x)
{
    h[x.cost%MOD].push_back(x);
}
int ver(str a,str b)
{
    if(a.o1==b.o1) return 1;
    if(a.o1==b.o2) return 1;
    if(a.o2==b.o1) return 1;
    if(a.o2==b.o2) return 1;
    if(b.o1>a.o2) return 1;
    return 0;
}
int egal(str a,str b)
{
    if(a.cost==l-b.cost) return 0;
    return 1;
}
int findit(str x)
{
    int ct=0;
    int key=(l-x.cost)%MOD;
    vector<str>::iterator it;
    for(it=h[key].begin();it!=h[key].end();++it)
    {
        if((egal(*it,x)==0)) ++ct;
    }
    return ct;
}
int main()
{
    freopen ("oite.in","r",stdin);
    freopen ("oite.out","w",stdout);
    scanf("%d%d",&c,&l);
    int ct=0;
    for(int i=1;i<=c;i++)
    {
        scanf("%d",&v[i]);
    }
    for(int i=1;i<=c;i++)
    {
        for(int j=i+1;j<=c;j++)
        {
            str x;
            x.o1=i;
            x.o2=j;
            x.cost=v[i]+v[j];
            ct+=findit(x);
        }
        for(int j=1;j<i;j++)
        {
            str x;
            x.o1=i;
            x.o2=j;
            x.cost=v[i]+v[j];
            addtohash(x);
        }
    }
    printf("%d\n",ct);
}