C11LOCK - Ổ khóa đặc biệt
Tác giả: ladpro98
Ngôn ngữ: Pascal
program c11lock;
uses math;
const fi='';
maxn=505;
type arr=array[0..maxn*maxn] of longint;
var a:array[1..5,1..maxn] of longint;
s1,s2:arr;
n,d:longint;
kk,res:int64;
procedure input;
var inp:text;
i,j:longint;
begin
assign(inp,fi);reset(inp);
readln(inp,n,kk);
for i:=1 to 5 do
begin
for j:=1 to n do read(inp,a[i,j]);
readln(inp);
end;
close(inp);
end;
procedure sort(var sum:arr; l,r:longint);
var i,j,p,t:longint;
begin
if l>=r then exit;
i:=l;j:=r;
p:=sum[random(r-l+1)+l];
repeat
while sum[i]<p do inc(i);
while sum[j]>p do dec(j);
if i<=j then
begin
if i<j then
begin
t:=sum[i];
sum[i]:=sum[j];
sum[j]:=t;
end;
inc(i);dec(j);
end;
until i>j;
sort(sum,l,j);sort(sum,i,r);
end;
procedure process;
var t:int64;
i,j,k,l,r:longint;
begin
for i:=1 to n do
for j:=1 to n do
begin
inc(d);
s1[d]:=a[1,i]+a[2,j];
s2[d]:=a[3,i]+a[4,j];
end;
sort(s1,1,d);sort(s2,1,d);
for k:=1 to n do
begin
r:=d;l:=d;
for i:=1 to d do
begin
t:=kk-a[5,k]-s1[i];
while (r>0) and (t<s2[r]) do dec(r);
while (l>0) and (t<=s2[l]) do dec(l);
if (r>0) and (t=s2[r]) then inc(res,r-l);
end;
end;
end;
begin
input;
process;
write(res);
end.