Pagini recente » Cod sursa (job #2084991) | Cod sursa (job #1655915) | Cod sursa (job #2380668) | Cod sursa (job #824513) | Cod sursa (job #53850)
Cod sursa(job #53850)
#include<stdio.h>
#include<vector>
using namespace std;
const int maxn = 1025;
const int tabm = 666013;
int i;
int n;
int k;
int j;
int a[maxn];
long long sol;
vector<int> vect[tabm];
vector<int> vect2[tabm];
int ta[tabm];
int ta2[tabm];
int hash(int i)
{
int h = (i << 4) ^ (i >> 28);
if (h < 0) h *= -1;
h %= tabm;
return h;
}
int main()
{
freopen("oite.in","r",stdin);
freopen("oite.out","w",stdout);
scanf("%d %d",&n,&k);
for(i = 1;i <= n; ++i)
{
scanf("%d",&a[i]);
}
for(i = 1;i <= n; ++i)
{
for(j = 1;j < i; ++j)
{
if (a[i] + a[j] <= k)
{
// int aux = sol;
int nrsol = 0;
int k1 = 0;
for(k1 = 0; k1 < ta[hash(k - a[i] - a[j])]; ++k1)
{
if (vect[hash(k - a[i] - a[j])][k1] == k - a[i] - a[j]) nrsol++;
}
sol += (long long)nrsol;
// if (ta[hash(k - a[i] - a[j])] > 0)
/* {
printf("%d\n",k - a[i] - a[j]);
}*/
nrsol = 0;
if (a[i] + a[j] * 2 <= k)
{
for(k1 = 0; k1 < ta2[hash(k - a[i] - 2 * a[j])]; ++k1)
{
if (vect2[hash(k - a[i] - 2 * a[j])][k1] == k - a[i] - 2 * a[j]) nrsol++;
}
sol -= nrsol;
if (k - a[i] - a[j] * 2 == a[j]) (long long)sol++;
}
//if (sol < 0) printf("%d %d\n",i,j);
// if (sol > aux) printf("%d %d %d\n",i,j,sol - aux);
}
}
// printf("%d %d\n",i,sol);
for(j = 1;j < i; ++j)
{
ta[hash(a[i] + a[j])]++;
vect[hash(a[i] + a[j])].push_back(a[i] + a[j]);
}
ta2[hash(a[i])]++;
vect2[hash(a[i])].push_back(a[i]);
}
printf("%lld\n",sol / 3);
return 0;
}