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 Access Violation?
Posted by ILTK! 23 Jul 2004 11:36
program d1013;
{$APPTYPE CONSOLE}
type
  longnum=array[1..3000] of longint;
function getlen(num:longnum):longint;
var
  i:longint;
begin
  i:=3000;
  while (i>=1) do
    if  (num[i]=0)  then
      dec(i)
    else break;
  getlen:=i;
end;
function sum(num1,num2:longnum):longnum;
var
  h,i,l1,l2,ost:longint;
begin
  l1:=getlen(num1);
  l2:=getlen(num2);
  ost:=0;
  if l1>l2 then
  begin
    for i:=1 to l1 do
    begin
      h:=num1[i];
      num1[i]:=(num1[i]+num2[i]+ost) mod 10;
      ost:=(h+num2[i]+ost) div 10;
    end;
    num1[l1+1]:=num1[l1+1]+ost;
    sum:=num1;
  end
  else
  begin
    for i:=1 to l2 do
    begin
      h:=num2[i];
      num2[i]:=(num2[i]+num1[i]+ost) mod 10;
      ost:=(h+num1[i]+ost) div 10;
    end;
    num2[l2+1]:=num2[l2+1]+ost;
    sum:=num2;
  end;
end;
function mul(k:longint; num:longnum):longnum;
var
  h,ost,l1,i:longint;
begin
  l1:=getlen(num);
  ost:=0;
  for i:=1 to l1 do
  begin
    h:=num[i];
    num[i]:=(num[i]*k+ost) mod 10;
    ost:=(h*k+ost) div 10;
  end;
  num[l1+1]:=ost;
  mul:=num;
end;
var
  n,k,i:longint;
  work1,work2,work3,work:longnum;
begin
  readln(n,k);
  if (n<2) or (k<2) then
    halt;
  if k>10 then
  begin
    i:=n;
    n:=k;
    k:=i;
  end;
 { if k=1 then
  begin
    write(0);
  end;}
  fillchar(work1,sizeof(work1),0);
  fillchar(work2,sizeof(work2),0);
  fillchar(work3,sizeof(work3),0);
  work1[1]:=k mod 10;
  work1[2]:=k div 10;
  work2[1]:=((k-1)*(k+1))mod 10;
  work2[2]:=((k-1)*(k+1))div 10;
  for i:=3 to n-1 do
  begin
    work3:=mul((k-1),sum(work1,work2));
    work1:=work2;
    work2:=work3;
  end;
  fillchar(work,sizeof(work),0);
  if n>3 then
  begin
    work:=mul(k-1,work3);
  end
  else
    if n=3 then
      work:=mul(k-1,work2)
    else
      work:=mul(k-1,work1);
  k:=getlen(work);
  for i:= k downto 1 do
    write(work[i]);
end.
Re: Why Access Violation?
Posted by Kraev Aleksey 24 Jul 2004 17:54
Thank you for help. I understand my mistake.