比赛 20120705 评测结果 C
题目名称 数字计算 最终得分 0
用户昵称 czp 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2012-07-05 09:55:39
显示代码纯文本
var
 f,g:array[0..22,0..22,0..202] of longint;
 o:array[0..22,0..22,0..202] of boolean;
 a,ss:array[0..22] of longint;
 i,j,t,n,k,l,ans:longint;
 s:string;
function min(xx,yy:longint):longint;
begin
if xx>yy then min:=yy else min:=xx;
end;
function date(x,y:longint):longint;
 var i:longint;
 begin
  date:=0;
  for i:=x to y do
   begin
    date:=date*10;
    date:=date+a[i];
    if date>200 then break;
   end;
 end;
 procedure dfs(j,v,p,dd:longint);
  begin
   if v>200 then exit;
   if dd>200 then exit;
   if  j=n then begin
    if v*dd<=200 then f[i,j,v*dd]:=min(p,f[i,j,v*dd]);
    exit;
    end;
   if v*dd<=200 then f[i,j,v*dd]:=min(p,f[i,j,v*dd]);
   dfs(j+1,v,p,dd*10+a[j+1]);
   dfs(j+1,v*dd,p+1,a[j+1]);
  end;
  procedure dfs1(l,r,t:longint);
  var i,j:longint;
  begin
   if o[l,r,t] then exit;
   o[l,r,t]:=true;
   g[l,r,t]:=f[l,r,t];
   if l=r then  exit;
   for i:=0 to t do
    for j:=l to r-1 do
     begin
      dfs1(l,j,i);
      dfs1(j+1,r,t-i);
             if f[l,j,i]+f[j+1,r,t-i]+1<g[l,r,t] then
      g[l,r,t]:=f[l,j,i]+f[j+1,r,t-i]+1;
             if f[l,j,i]+g[j+1,r,t-i]+1<g[l,r,t] then
      g[l,r,t]:=f[l,j,i]+g[j+1,r,t-i]+1;
             if g[l,j,i]+f[j+1,r,t-i]+1<g[l,r,t] then
      g[l,r,t]:=g[l,j,i]+f[j+1,r,t-i]+1;
             if g[l,j,i]+g[j+1,r,t-i]+1<g[l,r,t] then
      g[l,r,t]:=g[l,j,i]+g[j+1,r,t-i]+1;
     end;
  end;
begin
 assign(input,'puzzle.in');reset(input);
 assign(output,'puzzle.out');rewrite(output);
 repeat
 readln(s);
 readln(t);
 if t<0 then break;
 n:=length(s);
 for i:=1 to n do
  a[i]:=ord(s[i])-ord('0');
 fillchar(f,sizeof(f),1);
 fillchar(g,sizeof(g),1);
 fillchar(o,sizeof(o),0);
 fillchar(ss,sizeof(ss),0);
 for i:=1 to n do
  for j:=i to n do
   begin
    k:=date(i,j);
    if k<=200 then
     f[i,j,k]:=0;
   end;
 for i:=1 to n-1 do
  dfs(i,1,0,a[i]);
 for i:=1 to n do
  begin
   ss[i]:=ss[i-1];
   if a[i]=0 then ss[i]:=ss[i]+1;
  end;
 for i:=1 to n-1  do
  for j:=i+1 to n do
   begin
    if (a[i]=0) or (a[j]=0) then f[i,j,0]:=min(1,f[i,j,0]);
    if ss[j]-ss[i-1]>0 then f[i,j,0]:=min(2,f[i,j,0]);
   end;
 dfs1(1,n,t);
 if g[1,n,t]>100000 then writeln(-1) else writeln(g[1,n,t]);
 until 1>2;
 close(input);close(output);
end.