Cod sursa(job #55775)

Utilizator info_arrandrei gigea info_arr Data 28 aprilie 2007 13:22:09
Problema Next Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.76 kb
{$IFDEF NORMAL}
  {$I-,Q-,R-,S-}
{$ENDIF NORMAL}
{$IFDEF DEBUG}
  {$I+,Q+,R+,S-}
{$ENDIF DEBUG}
{$IFDEF RELEASE}
  {$I-,Q-,R-,S-}
{$ENDIF RELEASE}


const dmax=800000;
      base=10;

type bignum=record
       n:longint;
       x:array[1..dmax] of integer;
     end;

var i,j:longint;
    s:string[30];
    n,sol,a:bignum;
    d,r:int64;
    e:integer;
    fi,fo:text;
    c:char;

 procedure rest;
  var i:longint;
      t:int64;
   begin
   t:=0;
    for i:=n.n downto 1 do
     t:=(t*10+n.x[i]) mod d;
   r:=t;
   end;


 procedure invers(var n:bignum);
  var i,aux:longint;
  begin
  for i:=1 to n.n div 2 do
   begin aux:=n.x[i]; n.x[i]:=n.x[n.n-i+1]; n.x[n.n-i+1]:=aux; end;
  end;


 function max(a,b:longint):longint;
  begin if a>b then max:=a else max:=b; end;


 procedure adun;

  var t,i:longint;
 begin
  fillchar(sol,sizeof(sol),0);
  sol.n:=max(a.n,n.n);
  for i:=1 to sol.n do
   begin
   t:=t+a.x[i]+n.x[i];
   sol.x[i]:=t mod base;
   t:=t div base;
  end;
  if (t>0) then begin
   inc(sol.n);
   sol.x[sol.n]:=t;
  end;
 end;

 procedure write_big;
  var s:string;
      i,j:longint;
  begin
   write(fo,sol.x[sol.n]:0);
   for i:=sol.n-1 downto 1 do
    begin
     str(sol.x[i]:1,s);
     for j:=1 to 1 do if s[j]=' ' then s[j]:='0';
    write(fo,s);
   end;
  writeln(fo);
  end;



begin
assign(fi,'next.in'); reset(fi);
assign(fo,'next.out'); rewrite(fo);
read(fi,c);
i:=0;
while (c in ['0'..'9']) do
 begin
  s:=c;
  inc(i);
  val(s,n.x[i],e);
  read(fi,c);
 end;
n.n:=i;
invers(n);
readln(fi);
readln(fi,s);
val(s,d,e);
rest;
d:=(d-r) mod d;
i:=0;
while (d<>0) do
begin
 e:=d mod 10;
 inc(i);
 a.x[i]:=e;
 d:=d div 10;
end;
a.n:=i;
adun;
write_big;
close(fo);
end.