这是一道状压DP,首先这道题让我意识到状态是从 1 to (1<<n)-1 的,所以当前加入的某头牛编号是从 0 to n-1 的,所以存储的时候习惯要改一下,这样子做状压DP才会顺一点吧。
PS:一定要仔细看题目,题目提醒了不会超过64位,然而悲催的没看见,于是第一次就WA了=-=//。
1 const maxn=1<<16; 2 var n,i,j,k,max:longint; 3 ans:int64; 4 s:array[0..16] of longint; 5 f:array[0..16,0..maxn] of int64; 6 begin 7 readln(n,max); 8 dec(n); 9 for i:=0 to n do readln(s[i]); 10 for i:=0 to n do f[i,1<<i]:=1; 11 for i:=0 to 1<<(n+1)-1 do 12 for j:=0 to n do 13 if (i and (1<<j))>0 then 14 for k:=0 to n do 15 if (i and (1<<k)=0) and (abs(s[j]-s[k])>max) then 16 inc(f[k,i or (1<<k)],f[j,i]); 17 for i:=0 to n do 18 inc(ans,f[i,1<<(n+1)-1]); 19 writeln(ans); 20 end.
时间: 2024-10-05 05:05:46