其实这道题只告诉了一个事
当出现多个满足答案约束条件是,我们可以求一个再求一个,不要一下子全求完
前两个条件怎么弄之前已经做过类似的了
于是我们可以用记忆化搜索找出最小差异
然后配合最小差异来剪枝,搜索出最小答案
var a,b:array[..] of longint;
f:array[..,..] of longint;
n,m,k,i:longint;
can:boolean;
s:string; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; function differ(j,cur:longint):longint; //f[j,cur]表示处理到第j位前面余数为cur的情况最小差异数
var i,wh,s:longint;
begin
if (j=n+) then
begin
if cur= then exit()
else exit();
end;
if f[j,cur]<>- then exit(f[j,cur]);
if j<> then s:= else s:=;
f[j,cur]:=;
for i:=s to do
begin
wh:=differ(j+,(cur*+i) mod m);
if i<>a[j] then inc(wh);
f[j,cur]:=min(f[j,cur],wh);
end;
exit(f[j,cur]);
end; procedure dfs(j,wh,cur:longint);
var i,s:longint;
begin
if (j=n+) then
begin
if cur= then can:=true;
exit;
end;
if can then exit;
if f[j,cur]+wh>k then exit; //剪枝
if j<> then s:= else s:=;
for i:=s to do
begin
b[j]:=i;
if (i=a[j]) then
dfs(j+,wh,(cur*+i) mod m)
else
dfs(j+,wh+,(cur*+i) mod m);
if can then exit;
end;
end; begin
while not eof do
begin
readln(s);
n:=length(s);
for i:= to n do
a[i]:=ord(s[i])-;
readln(m);
if s='' then
begin
writeln();
continue;
end;
fillchar(f,sizeof(f),);
k:=differ(,);
// writeln(k);
can:=false;
dfs(,,);
for i:= to n do
write(b[i]);
writeln;
end;
end.