Pagini recente » Cod sursa (job #1991261) | Cod sursa (job #2175313) | Cod sursa (job #1560547) | Cod sursa (job #1828826) | Cod sursa (job #244432)
Cod sursa(job #244432)
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 1024, HSIZE = 20423;
const double knt = 0.61803398;
int N,S,c[NMAX];
struct node {int fi,se,th;};
vector< node > hash[HSIZE];
node make_pair(int a,int b,int c) {
node tmp = {a,b,c};
return tmp;
}
inline int h(int x) {
return int( (double(x * knt) - int(x * knt)) * (HSIZE-1) );
}
int search(int x,int ind)
{
int hh = h(x);
for(int i = 0; i < (int)hash[hh].size(); ++i)
if(hash[hh][i] . fi == x && hash[hh][i] . th == ind) return i;
return -1;
}
void insert(int x,int ind) {
int hh = h(x), t = search(x,ind);
if(t == -1)
hash[hh].push_back(make_pair(x,1,ind));
else hash[hh][t] . se ++;
}
int count(int x,int ind) {
if(x<0) return 0;
int cnt = 0,hh = h(x);
for(int i = 0; i < (int)hash[hh].size(); ++i)
if(hash[hh][i] . fi == x && hash[hh][i] . th > ind)
cnt += hash[hh][i] . se;
return cnt;
}
int main() {
freopen("oite.in","r",stdin);
freopen("oite.out","w",stdout);
scanf("%d%d",&N,&S);
for(int i=1;i<=N;++i)
scanf("%d",&c[i]);
for(int i=1;i<N;++i)
{
for(int j=i+1;j<=N;++j)
insert(c[i]+c[j],i);
}
int cnt = 0;
for(int i=1;i<N;++i)
for(int j=i+1;j<=N;++j)
cnt += count(S - c[i] - c[j],j);
printf("%d",cnt);
return 0;
}