Cod sursa(job #975283)

Utilizator t.g.g.tt.g.g.t t.g.g.t Data 19 iulie 2013 17:59:00
Problema Cifre Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.75 kb
var a:array[0..9,0..9]of longint;
    i,j,st2,dr2,c,k:longint;
    stotal:longint;

procedure calc(st,dr,r:longint;k,v,p:longint);
   var z,i,j,z2:longint;
  begin
    while ((st div r)=(dr div r)) and (v>1) do
      begin
        if (p=1)or(c<>0) then if c=(st div r) then begin dec(k); end; if k<0 then k:=0;
        if st div r<>0 then p:=1;
        st:=st mod r;
        dr:=dr mod r;
        dec(v);
        r:=r div 10;
      end;
    i:=st div r; j:=dr div r;
    if v=1 then
      begin
        for z:=st to dr do if z=c then begin if k<=1 then inc(stotal); end else if k<=0 then inc(stotal);
      end
    else if v>1 then
      begin

        if ((i+1)<=(j-1)) then
          begin
            for z:=k to 9 do stotal:=stotal+a[v-1,z]*(j-i-1);
            if (c in [(i+1)..(j-1)]) and (k>0) then stotal:=stotal+a[v-1,k-1];
          end;
        z:=0; z2:=0;
        if (p=1) or (c<>0) then if i=c then begin z:=1; dec(k); if k<0 then begin k:=0; z:=0; end; end;
        if i<>0 then begin if p=0 then z2:=1; p:=1; end;
        calc(st mod r,r-1,r div 10,k,v-1,p); if z=1 then inc(k);  if z2=1 then p:=0;
        if (p=1) or (c<>0) then if j=c then begin dec(k); if k<0 then k:=0; end;
        if j<>0 then begin p:=1; end;
        calc(0,dr mod r,r div 10,k,v-1,p);
      end;
  end;

begin
  a[1,0]:=9; a[1,1]:=1; a[0,0]:=1;
  for i:=1 to 8 do begin for j:=0 to 8 do begin a[i+1,j]:=a[i+1,j]+a[i,j]*9; a[i+1,j+1]:=a[i,j]; end; a[i+1,9]:=a[i+1,9]+a[i,9]*9; end;
  assign(input,'cifre.in'); reset(input);
  readln(st2,dr2,c,k);
  i:=1; j:=0;  assign(output,'cifre.out'); rewrite(output);
  while (dr2 div i)>=10 do begin i:=i*10; inc(j); end;
  calc(st2,dr2,i,k,j+1,0);
  writeln(stotal/(dr2-st2+1):0:4);
  close(output);
end.