比赛 20120705 评测结果 WWWWWWWWWW
题目名称 数字计算 最终得分 0
用户昵称 isabella 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2012-07-05 11:57:56
显示代码纯文本
const
 inf=90000000;
var
 f:array[1..20,0..200,0..19]of longint;
 map:array[1..20,1..20,0..19,0..200]of int64;
 va:array[1..20,1..20]of int64;
 v:array[0..200]of boolean;
 s:string;
 i,j,n,m,k,l,p,q,t,ans:longint;

 function min(a,b:longint):longint;
  var temp:longint;
  begin
   if a<b then temp:=a
    else temp:=b;
    exit(temp);

  end;

begin
assign(input,'puzzle.in');reset(input);
assign(output,'puzzle.out');rewrite(output);
 readln(s);n:=length(s);
 readln(t);
while t>=0 do begin
 for i:=1 to n do
  begin
   va[i,i]:=ord(s[i])-48;
   map[i,i,0,0]:=1;map[i,i,0,1]:=va[i,i];
   for j:=i+1 to n do
    if va[i,j-1]<inf then va[i,j]:=va[i,j-1]*10+ord(s[j])-48 else va[i,j]:=inf;
  end;

 for l:=2 to n do
 for i:=1 to n+1-l do begin
   j:=i+l-1;
   map[i,j,0,0]:=1;map[i,j,0,1]:=va[i,j];
   for p:=1 to l-1 do begin   //map[i,j,p];
    fillchar(v,sizeof(v),0);
    for k:=i+p-1 to j-1 do //map[i,k,p-1]
     for q:=1 to map[i,k,p-1,0] do
      if map[i,k,p-1,q]*va[k+1,j]<=t then v[map[i,k,p-1,q]*va[k+1,j]]:=true;

    for k:=0 to t do if v[k] then
     begin inc(map[i,j,p,0]);map[i,j,p,map[i,j,p,0]]:=k;end;
   end;

 end;

 {for i:=1 to n do
  for j:=i to n do
   for p:=0 to j-i do
   begin
    write(i,' ',j,' ',p,':');
    for k:=1 to map[i,j,p,0] do write(map[i,j,p,k],' ');writeln;
   end;   }

 fillchar(f,sizeof(f),$7f);
 for i:=1 to n do
  begin
   if va[1,i]<=t then f[i,va[1,i],0]:=0;

   for p:=1 to i-1 do
    for q:=1 to map[1,i,p,0] do
      if map[1,i,p,q]<=t then f[i,map[1,i,p,q],0]:=min(f[i,map[1,i,p,q],0],p);
  end;

 for i:=1 to n do
  for j:=0 to t do
   for m:=1 to i-1 do   //f[i,j,m];
    begin
      for k:=m to i-1 do
       for p:=0 to i-k-1 do
        for q:=1 to map[k+1,i,p,0] do
         begin
          if (i=3)and(j=5)and(m=1)and(k=2)and(p=0)and(q=1)then
            begin
              s:='l';
            end;
          if j>=(map[k+1,i,p,q]) then
           f[i,j,m]:=min(f[i,j,m],f[k,j-map[k+1,i,p,q],m-1]+p);
          s:='s';
         end;
    end;

 ans:=maxlongint; //f[n,t,i]
 for i:=0 to n-1 do
  if f[n,t,i]+i<ans then
   begin
    //writeln(i,' ',f[n,t,i]);
    ans:=f[n,t,i]+i;
   end;
 if ans>n-1 then writeln(-1) else writeln(ans);

 readln(s);n:=length(s);
 readln(t);
end;
close(input);close(output);
end.