Cod sursa(job #974884)

Utilizator t.g.g.tt.g.g.t t.g.g.t Data 18 iulie 2013 17:06:42
Problema Cifre Scor 90
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.43 kb
var a:array[0..9,0..9]of longint;
    i,j,st2,dr2,c,k:longint;
    st,dr:array[1..10]of byte;
    stotal:longint;

procedure calc(st,dr,r:longint;k,v,p:integer);
   var z,i,j:integer;
  begin
  while ((st div r)=(dr div r)) and (v>0) do begin if st div r=c then dec(k); if k<0 then k:=0; st:=st mod r; dr:=dr mod r; r:=r div 10; v:=v-1; end;
  if (v>0) then begin
    i:=st div r; j:=dr div r;
    if i+1<j-1 then for z:=k to 9 do stotal:=stotal+a[v,z]*(j-i-1);
    if (c>i) and (c<j) and (k>0) then stotal:=stotal+a[v,k-1];
    if p=0 then if i<>0 then p:=1;
    if i=c then begin if p=1 then z:=k-1 else z:=k end else z:=k; if z<0 then z:=0;
    calc(st mod r,r-1,r div 10,z,v-1,p);
    if p=0 then if j<>0 then p:=1;
    if j=c then begin if p=1 then  z:=k-1 else z:=k end else z:=k; if z<0 then z:=0;
    calc(0,dr mod r,r div 10,z,v-1,p);
  end else
    begin
      for z:=st to dr do if z=c then begin if k-1<=0 then inc(stotal) end else if k=0 then inc(stotal);
    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;
  while (dr2 div i)>=10 do begin i:=i*10; inc(j); end;
  calc(st2,dr2,i,k,j,0);
  assign(output,'cifre.out'); rewrite(output);
  writeln(stotal/(dr2-st2+1):0:4);
  close(output);
end.