Cod sursa(job #53205)

Utilizator info_arrandrei gigea info_arr Data 21 aprilie 2007 14:38:32
Problema Next Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.82 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=1000000;
      base=10;

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

var n,sol,int,int1:bignum;
    d,r,a:int64;
    fi,fo:text;
    c:char;
    s:string[1];
    e,vl:integer;
    i,aux:longint;


 procedure rest;
  var i:longint;
      t:int64;
   begin
   t:=0;
    for i:=n.n downto 1 do
     t:=(t*base+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 i,t:longint;
 begin
  fillchar(sol,sizeof(sol),0);
  sol.n:=max(n.n,int.n);
  t:=0;
  for i:=1 to sol.n do
   begin
    t:=t+n.x[i]+int.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]);
   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:=1;
while (c in ['0'..'9']) do
 begin
 s:=c;
 val(s,vl,e);
 n.x[i]:=vl;
 inc(i);
 read(fi,c);
end;
n.n:=i-1;
invers(n);
readln(fi);
readln(fi,d);
rest;
a:=(d-r) mod d;
i:=1;
while a<>0 do
 begin
  aux:=a mod 10;
  int.x[i]:=aux;
  inc(i);
  a:=a div 10;
 end;
int.n:=i-1;
invers(int);
adun;
write_big;
close(fo);
end.