Pagini recente » Cod sursa (job #2254432) | Cod sursa (job #2265266) | Cod sursa (job #786358) | Cod sursa (job #2903922) | Cod sursa (job #316256)
Cod sursa(job #316256)
#include<stdio.h>
#define NMAX 1024
#define MOD 666013
struct nod
{
int sum;
nod *next;
};
typedef nod * pNode;
pNode LIST[MOD];
int N, A[NMAX], L,contor;
int find(int v)
{
pNode c=LIST[v%MOD];
if(!c)
return 0;
pNode p=c;
while(p)
{
if(p->sum==v)
return 1;
p=p->next;
}
return 0;
}
int find_old(int v)
{
int h=v%MOD;
for(nod *p=LIST[h]; p; p=p->next)
if(p->sum == v) return 1;
return 0;
}
void insert(int x)
{
pNode p;
int h=x%MOD;
if(!find(x))
{
p=new nod;
p->sum=x;
p->next=LIST[h];
LIST[h]=p;
}
}
void find_next(int x, int y)
{
int i,j,sum;
for(i=y+1;i<=N-1;i++)
for(j=i+1;j<=N;j++)
{
sum=A[i]+A[j];
if(A[x]+A[y]+sum==L)
{
contor++;
printf("%d %d %d %d\n",x,y,i,j);
}
}
}
int main()
{
freopen("oite.in","r",stdin);
freopen("oite.out","w",stdout);
scanf("%d %d",&N,&L);
int i,j,suma;
for(i=1;i<=N;i++)
{
scanf("%d ",&A[i]);
}
for(i=1;i<=N-1;i++)
for(j=i+1;j<=N;j++)
{
insert(A[i]+A[j]);
}
for(i=1;i<=N-1;i++)
for(j=i+1;j<=N;j++)
{
suma=L-A[i]-A[j];
if(suma>=0)
if(find(suma))
{
//printf("found\n");
find_next(i,j);
}
}
printf("%d\n",contor);
return 0;
}