ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1012. K-based Numbers. Version 2

Why do I get Wrong Answer ? Here is my program:
Posted by Costel::icerapper@k.ro 21 Feb 2002 15:24
PROGRAM SAVEMAN_TIMUS;
CONST
  MAXDIGIT      =       180;
  NODEDIGIT     =         8;
  MAXVALUE      =  99999999;
  MODULATOR     = 100000000;
TYPE
  BigNumber=array[1..MAXDIGIT div NODEDIGIT]of longint;

PROCEDURE INIT(var b:BigNumber);
          begin  fillchar(b,sizeof(b),0);end;

PROCEDURE ADDINT(var b:BigNumber;int:longint);
          var  i:longint; begin
          for i:=1 to MAXDIGIT div NODEDIGIT do
          begin    b[i]:=b[i]+int;    int:=b[i] div MODULATOR;
          b[i]:=b[i] mod MODULATOR;  if int=0 then      exit;  end; end;

PROCEDURE ADDBIG(var b:BigNumber;n:BigNumber);
          var  i:longint;  REST:longint;begin REST:=0;
          for i:=1 to MAXDIGIT div NODEDIGIT do  begin b[i]:=b[i]+n[i]+REST;
          REST:=b[i] div MODULATOR; b[i]:=b[i] mod MODULATOR;  end;end;

PROCEDURE MULINT(var b:BigNumber;int:longint);
          var i:longint;REST:longint;begin for i:=1 to MAXDIGIT div NODEDIGIT do
          begin b[i]:=b[i]*int+REST; REST:=b[i] div MODULATOR;b[i]:=b[i] mod MODULATOR;
          end;end;

PROCEDURE PRINTBIG(b:BigNumber);
          var k,i,l:longint; s:string;begin for i:=MAXDIGIT div NODEDIGIT downto 1 do
          if b[i]<>0 then break; write(b[i]); k:=i-1; for i:=k downto 1 do
          begin   Str(b[i]:8,s);    l:=1;   while (s[l]=' ')and(l<=8)
          do begin s[l]:='0';inc(l);end; write(s); end; end;

var
  A0,B0,A1:BigNumber;
  n,k:longint;
  i:longint;
begin
  readln(n,k);  init(a0);  init(b0);  ADDINT(b0,k-1);
  for i:=2 to n do begin
     A1:=B0;        {a(i)=b(i-1)}
     ADDBIG(b0,a0); {b(i)=b(i-1)+a(i-1)}
     MULINT(b0,k-1);{b(i)=b(i-1)*(k-1)}
     A0:=A1;        {inc(i)}
   end;
  ADDBIG(A1,B0);    {a(n)=a(n)+b(n)}
  PRINTBIG(A1);
end.
You have overflow and carry loosing in MULINT and other
Posted by Leo 20 Nov 2002 13:10
b[i]*int may be more than MaxLongInt