MDIGITS - Counting Digits

Tác giả: ll931110

Ngôn ngữ: Pascal

Program MDIGITS;
        Const
                input  = '';
                output = '';
                  maxd = 9;
        Var
                fi,fo: text;
                    p: array[0..maxd] of longint;
                    F: array[0..9,0..9] of longint;
             da,db,dc: array[0..maxd] of longint;
                a,b,t: longint;

Procedure openfile;
          Begin
                Assign(fi, input);
                        Reset(fi);

                Assign(fo, output);
                        Rewrite(fo);
          End;

Procedure gens;
          Var
                i,j: longint;
          Begin
                p[0]:= 1;
                For i:= 1 to maxd do p[i]:= p[i - 1] * 10;

                For i:= 0 to 9 do F[i,1]:= 1;
                For i:= 2 to 9 do
                  For j:= 0 to 9 do F[j,i]:= F[j,i - 1] * 10 + p[i - 1];
          End;

Procedure solve(x: longint);
          Var
                i,j,k,digit: longint;
          Begin
                Fillchar(dc, sizeof(dc), 0);
                If x < 10 then
                  Begin
                      For i:= 0 to x do dc[i]:= 1;
                      exit;
                  End;

                digit:= 1;
                While p[digit] <= x do inc(digit);

                dc[0]:= 0;
                For i:= 1 to digit - 1 do dc[0]:= dc[0] - p[i];

                While digit >= 1 do
                  Begin
                        k:= 0;
                        While (k + 1) * p[digit - 1] <= x do
                          Begin
                            dc[k]:= dc[k] + p[digit - 1];
                            For j:= 0 to 9 do dc[j]:= dc[j] + F[j,digit - 1];
                            inc(k);
                          End;

                        x:= x - p[digit - 1] * k;
                        dc[k]:= dc[k] + x + 1;
                        dec(digit);
                  End;
          End;

Procedure printresult;
          Var
                i: longint;
          Begin
                solve(a - 1);
                da:= dc;

                solve(b);
                db:= dc;

                For i:= 0 to 9 do write(fo, db[i] - da[i], ' ');
                Writeln(fo);
          End;

Procedure closefile;
          Begin
                Close(fo);
                Close(fi);
          End;

Begin
        openfile;
        gens;

        Repeat
             Read(fi, a, b);
             If a > b then
                Begin
                        t:= a;
                        a:= b;
                        b:= t;
                End;
             If (a <> 0) or (b <> 0) then printresult;
        Until (a = 0) and (b = 0);

        closefile;
End.

Download