这是一道状压DP,首先这道题让我意识到状态是从 1 to (1<<n)-1 的,所以当前加入的某头牛编号是从 0 to n-1 的,所以存储的时候习惯要改一下,这样子做状压DP才会顺一点吧。
PS:一定要仔细看题目,题目提醒了不会超过64位,然而悲催的没看见,于是第一次就WA了=-=//。
const maxn=<<;
var n,i,j,k,max:longint;
ans:int64;
s:array[..] of longint;
f:array[..,..maxn] of int64;
begin
readln(n,max);
dec(n);
for i:= to n do readln(s[i]);
for i:= to n do f[i,<<i]:=;
for i:= to <<(n+)- do
for j:= to n do
if (i and (<<j))> then
for k:= to n do
if (i and (<<k)=) and (abs(s[j]-s[k])>max) then
inc(f[k,i or (<<k)],f[j,i]);
for i:= to n do
inc(ans,f[i,<<(n+)-]);
writeln(ans);
end.
(转载请注明出处:http://www.cnblogs.com/Kalenda/)