MDIGITS - Counting Digits

Tác giả: RR

Ngôn ngữ: Pascal

//Written by RR
{$r-,q-}
{$mode objfpc}
{$inline on}

uses math;
const
  FINP          =       '';
  FOUT          =       '';
var
  f1,f2         :       text;
  a,b,now       :       longint;
  count         :       array[0..9] of int64;

procedure openF;
    begin
      assign(f1,FINP); reset(f1);
      assign(f2,FOUT); rewrite(f2);
    end;
procedure closeF;
    begin
      close(f1);
      close(f2);
    end;
procedure solve(n:longint);
    var
      last,i,m:longint;
    begin
      while (n>0) do
        begin
          last:=n mod 10; n:=n div 10;
          for i:=1 to last do inc(count[i],now);
          m:=n;
          while (m>0) do
            begin
              inc(count[m mod 10],(last+1)*now);
              m:=m div 10;
            end;
          for i:=0 to 9 do inc(count[i],n*now);
          now*=10; dec(n);
        end;
    end;

begin
  openF;
  read(f1,a,b);
  while (a>0) or (b>0) do
    begin
      fillchar(count,sizeof(count),0);
      now:=1;  solve(max(a,b));
      now:=-1; solve(min(a,b)-1);
      write(f2,count[0]);
      for a:=1 to 9 do
        write(f2,' ',count[a]);
      writeln(f2);
      read(f1,a,b);
    end;
  closeF;
end.

Download