P1231: [Usaco2008 Nov]mixup2 混乱的奶牛

这是一道状压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.
View Code

(转载请注明出处:http://www.cnblogs.com/Kalenda/)

posted @ 2015-09-20 12:40  LovelyMonster丶  阅读(231)  评论(0编辑  收藏  举报