[BZOJ1087] [SCOI2005]互不侵犯King

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=1087

题目大意

8不连通的放法

题解

状压…

var
 sum,x:array[0..200]of longint;
 y:array[0..200,0..200]of longint;
 dp:array[0..10,0..100,0..100]of int64;
 i,j,k,l:longint;
 n,m,t:longint;
 ans:int64;
procedure prepare;
var a,b,c,summ:longint;
begin
 t:=1;
 for i:=0 to (1<<n)-1 do
  begin
   l:=1; summ:=0;
   for j:=1 to n do
    begin
     a:=0; b:=0;
     if i and(1<<(j-1))=0 then continue else inc(summ);
     if i>1 then a:=i and(1<<(j-2));
     if i<n then b:=i and(1<<(j));
     if (a<>0)or(b<>0) then begin l:=0; break; end;
    end;
   if l=1 then begin x[t]:=i; sum[t]:=summ; inc(t); end;
  end;
 dec(t);
 for i:=1 to t do
  dp[1,i,sum[i]]:=1;
 for i:=1 to t do
  for j:=1 to t do
   begin
    y[i,j]:=1;
    for k:=1 to n do
     begin
      a:=0; b:=0; c:=0;
      if x[i] and(1<<(k-1))=0 then continue;
      if k>1 then a:=x[j] and(1<<(k-2));
      if k<n then b:=x[j] and(1<<(k));
      c:=x[j] and(1<<(k-1));
      if (a<>0)or(b<>0)or(c<>0) then begin y[i,j]:=0; break; end;
     end;
   end;
end;

begin
 readln(n,m);
 prepare;
 for i:=2 to n do
  for j:=1 to t do
   for k:=sum[j] to m do
    begin
     dp[i,j,k]:=0;
     for l:=1 to t do
      if y[l,j]=1
      then dp[i,j,k]:=dp[i,j,k]+dp[i-1,l,k-sum[j]];
    end;
 ans:=0;
 for j:=1 to t do
  ans:=ans+dp[n,j,m];
 writeln(ans);
end.

你可能感兴趣的:([BZOJ1087] [SCOI2005]互不侵犯King)