Problem:
Coin Combination Problem

You have 4 bags A, B, C and D each of which includes N coins (there are totally 4N coins). Values of the coins in each bag are ai, bi, ci and di respectively.

Find the number of combinations that result when you choose one coin from each bag (totally 4 coins) in such a way that the total value of the coins is V. You should distinguish the coins in a bag.

Constraints

1 ≤ N ≤ 1000

1 ≤ ai, bi, ci, di ≤ 1016

1 ≤ V ≤ 1016

All input values are given in integers

Input

The input is given in the following format.

N V
a1 a2 ... aN
b1 b2 ... bN
c1 c2 ... cN
d1 d2 ... dN

Output

Print the number of combinations in a line.

Sample Input 1

3 14
3 1 2
4 8 2
1 2 3
7 3 2

Sample Output 1

9

Sample Input 2

5 4
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

Sample Output 2

625

Solution in C (judge verdict: Accepted)

// AOJ DPL_4_A Coin Combination Problem
// 2018.5.9 bal4u
 
#include <stdio.h>
 
// 数値のハッシュ関数
#define HASHSIZ 3456781
typedef struct { long long v; int f; } HASH;
HASH hash[HASHSIZ+3], *hashend = hash+HASHSIZ;

int lookup(long long n)
{
	HASH *p = hash + (int)(n % HASHSIZ);
	while (p->v) {
		if (p->v == n) return p->f;
		if (++p == hashend) p = hash;
	}
	return 0;
}

void insert(long long n)
{
	HASH *p = hash + (int)(n % HASHSIZ);
	while (p->v) {
		if (p->v == n) { p->f++; return; }
		if (++p == hashend) p = hash;
	}
	p->v = n, p->f = 1;
}

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif
 
int in()
{
    int n = 0, c = gc();
    do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
    return n;
}

long long inLL()
{
    long long n = 0, c = gc();
    do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
    return n;
}

int N;
long long V, V2, V3;
long long a[1002], b[1002], c[1002], d[1002];
int wa, wb, wc, wd;

int input(long long *x)
{
	int i, w;
	long long t;

	w = 0; for (i = 0; i < N; i++) {
		t = inLL();
		if (t <= V3) x[w++] = t;
	}
	return w;
}

int main()
{
	int i, j;
	long long t, ans;

	N = in();
	V = inLL(), V2 = V - 2, V3 = V - 3;
	wa = input(a);
	wb = input(b);
	wc = input(c);
	wd = input(d);

	for (i = 0; i < wa; i++) for (j = 0; j < wb; j++) {
		t = a[i] + b[j];
		if (t <= V2) insert(t);
	}

	ans = 0;
	for (i = 0; i < wc; i++) for (j = 0; j < wd; j++) {
		t = c[i] + d[j];
		if (t <= V2) ans += lookup(V - t);
	}

	printf("%lld\n", ans);
	return 0;
}