Cod sursa(job #38894)

Utilizator raduzerRadu Zernoveanu raduzer Data 26 martie 2007 11:13:42
Problema Next Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.6 kb
var a:array[1..1000000]of integer;
    b:array[1..1000001]of int64;
    d:int64;
    z,x,i,j,e:longint;
    c:char;

function dmare:boolean;
var i:longint;
begin
     dmare:=false;
     if z>x then exit;
     if x>z then
     begin
          dmare:=true;
          exit;
     end;
     for i:=z downto 1 do
     begin
          if a[i]>b[i] then exit;
          if b[i]>a[i] then
          begin
               dmare:=true;
               exit;
          end;
     end;
     dmare:=true;
end;


procedure adunare;
var i:longint;
    t:int64;
begin
     t:=d;
     i:=0;
     while t>0 do
     begin
          i:=i+1;
          if i>x then x:=x+1;
          b[i]:=b[i]+t;
          t:=b[i] div 10;
          b[i]:=b[i] mod 10;
     end;
     if dmare then
     begin
          exit;
     end
     else adunare;
end;

begin
     assign(input,'next.in');
     reset(input);
     assign(output,'next.out');
     rewrite(output);
     while not eoln do
     begin
          read(c);
          z:=z+1;
          a[z]:=ord(c)-ord('0');
     end;
     for i:=1 to z div 2 do
     begin
          e:=a[i];
          a[i]:=a[z+1-i];
          a[z+1-i]:=e;
     end;
     readln;
     readln(d);
     if (d=1)or(d=2)and(z>6) then
     begin
          x:=z;
          for i:=1 to z do b[i]:=a[i];
     end;
     if d>2 then adunare;
     for i:=x downto 1 do
     begin
          if (i=1)and(d=2)and(a[z] mod 2=1)and(z>6) then
          begin
               write(b[i]+1);
               break;
          end;
          write(b[i]);
     end;
close(output);
end.