记录编号 39160 评测结果 AAAAAAAAAA
题目名称 数字计算 最终得分 100
用户昵称 Gravatarczp 是否通过 通过
代码语言 Pascal 运行时间 0.341 s
提交时间 2012-07-05 16:47:22 内存使用 0.93 MiB
显示代码纯文本
var
 f:array[0..22,0..22,0..292] of longint;
 g:array[0..22,0..292] of longint;
 o:array[0..22,0..22,0..292] of boolean;
 a,ss:array[0..22] of longint;
 i,j,t,n,k,l,ans,deep: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,pp:longint;
 begin
  pp:=0;
  for i:=x to y do
   begin
    pp:=pp*10;
    pp:=pp+a[i];
    if pp>t then break;
   end;
   date:=pp;
 end;
 procedure dfs(j,v,p,dd:longint);
  begin
   if v>t then exit;
   if dd>t then exit;
   if  j=n then begin
    if v*dd<=t then f[i,j,v*dd]:=min(p,f[i,j,v*dd]);
    exit;
    end;
   if v*dd<=t 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;
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<=t 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;
 g[0,0]:=-1;
 for i:=1 to n do
  for j:=0 to t do
   for k:=0 to i do
    for l:=0 to j do
        g[i,j]:=min(g[i,j],g[k,j-l]+f[k+1,i,l]+1);
  if g[n,t]>100000 then writeln(-1) else writeln(g[n,t]);
 until 1>2;
 close(input);close(output);
end.