# bzoj2064

f[x,y]表示初始状态选取状况为x，最终状态选取状况为y的时候，最多划分成的对数

 1 var w,f:array[0..1024,0..1024] of longint;
2     sum,a,b,c:array[0..1024] of longint;
3     n,m,i,j,s1,s2,s:longint;
4 function max(a,b:longint):longint;
5   begin
6     if a>b then exit(a) else exit(b);
7   end;
8
9 function dfs(x,y:longint):longint;
10   var i,j,k:longint;
11   begin
12     if (x=s1) then
13     begin
14       if y<>s2 then exit(-1000)
15       else exit(0);
16     end;
17     if (y=s2) then
18     begin
19       if s1=x then exit(0)
20       else exit(-1000);
21     end;
22     if f[x,y]<>-1 then exit(f[x,y]);  //记忆化
23     for i:=1 to s1 do
24     begin
25       if i and x=0 then
26       begin
27         k:=sum[i];
28         for j:=1 to c[k] do   //在最终状态中找一个和初始状态的组合和相等的组形成新的一对
29           if w[k,j] and y=0 then
30             f[x,y]:=max(f[x,y],dfs(x xor i,y xor w[k,j])+1);
31       end;
32     end;
33     exit(f[x,y]);
34   end;
35
36 begin
38   for i:=1 to n do
41   for i:=1 to m do
43   s1:=1 shl n-1;
44   s2:=1 shl m-1;
45   for i:=0 to s1 do
46     for j:=0 to n-1 do
47       if i and (1 shl j)>0 then
48         sum[i]:=sum[i]+a[j+1];
49   for i:=0 to s2 do
50   begin
51     s:=0;
52     for j:=0 to m-1 do
53       if i and (1 shl j)>0 then
54         s:=s+b[j+1];
55     inc(c[s]);   //感觉这种做法很像Meet in middle
56     w[s,c[s]]:=i;
57   end;
58   fillchar(f,sizeof(f),255);
59   writeln(n+m-2*dfs(0,0));
60 end.
View Code

posted on 2014-10-07 21:54  acphile  阅读(250)  评论(0编辑  收藏  举报