MIXUP2 - Đàn bò hỗn loạn

Tác giả: RR

Ngôn ngữ: Pascal

{
PROG: mixup2
LANG: PASCAL
ID: invinci3
}
const
  FINP='';
  FOUT='';
  MAXN=16;
var
  n,k:longint;
  kq:int64;
  s:array[0..MAXN] of longint;
  d:array[0..1 shl MAXN,1..MAXN] of int64;
procedure inp;
var
  f:text;
  i:longint;
begin
  assign(f,FINP); reset(f);
  readln(f,n,k);
  for i:=1 to n do
    readln(f,s[i]);
  close(f);
end;
procedure ans;
var
  f:text;
begin
  assign(f,FOUT); rewrite(f);
  writeln(f,kq);
  close(f);
end;
procedure solve;
var
  mask,last,next:longint;
begin
  for last:=1 to n do
    d[1 shl (last-1),last]:=1;
  for mask:=0 to 1 shl n do
    for last:=1 to n do
    if mask and (1 shl (last-1))>0 then
      for next:=1 to n do
        if (mask and (1 shl (next-1))=0) and (abs(s[next]-s[last])>k) then
          inc(d[mask or (1 shl (next-1)),next],d[mask,last]);
  kq:=0;
  for last:=1 to n do
    inc(kq,d[1 shl n-1,last]);
end;
begin
  inp;
  solve;
  ans;
end.

Download