SPOJ – DHTABLE2

Đề bài: http://vn.spoj.com/problems/DHTABLE2/

Thuật toán:

  • (đang cập nhập)

Code:

var t:int64;
    n:integer;
    f:text;
procedure nhap;
begin
   assign(f,'');reset(f);
   read(f,n,t);
   close(f);
end;
 
procedure xuli;
var i:longint;
    x,l:string;
    mu,k,m
    :int64;
begin
    assign(f,'');rewrite(f);
    mu:=1; i:=0;
    while t>9*mu*(i+1) do
          begin
              dec(t,9*mu*(i+1));
              inc(i);
              mu:=mu*10;
          end;
    inc(i);
    k:=mu+(t-1) div i;
    t:=t mod i;
    x:='';
    while (length(x)<n) and (k>0) do
        begin
            m:=k;
            str(m,l);
            x:=l+x;
            if t>0 then
               begin
                   delete(x,t+1,i-t);
                   t:=0;
               end;
            dec(k);
        end;
    while length(x)>n do delete(x,1,1);
    while length(x)<n do x:=' '+x;
    write(f,x);
    close(f);
end;
 
begin
  nhap;
  xuli;
end.