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

How to write Pascal solutions

Pascal programs are compiled on the server with the 32-bit compiler FreePascal 2.6.4, which is adjusted to be compatible with Borland Delphi and is invoked with the following parameters:

ppc386 %1 -WC -Ci-o-r-t- -Xs -Sdgich -Se10 -l- -vwnh -n -dONLINE_JUDGE

You can download the FreePascal and read online documentation on www.freepascal.org.

An example of solving a problem

This is a sample solution for the A + B problem in Pascal:

var
   a, b: integer;
begin
   readln(a, b);
   writeln(a + b);
end.

What's new in the 32-bit compiler compared to the 16-bit one

If you are used to writing programs for old 16-bit DOS compilers (for example, for Borland Pascal 7.0), then it would be useful for you to know the following:

  • It is allowed to use arrays of size greater than 64 KB.
  • The type integer is a 32-bit type and coincides with the type longint. The 16-bit signed integer type is called smallint.
  • Strings can be longer than 255 characters. Don't refer to the zero character in order to determine the length of a string; use the function length(s) for this. You can set the length of a string using the procedure setlength(s, n).

Also, there are many new keywords. The following words cannot be redefined or used as identifiers:

as               false                new
class            finalization         on
dispose          finally              property
except           initialization       raise
exit             is                   true
exports          library              try

In addition, the words protected and published are reserved within the description of an object. Also, the name result in the body of a function is reserved: the value of the corresponding variable is the result of the operation of the function, and a declaration of this name will lead to a compilation error. For example, the following definitions of functions are equivalent:

function Sum(a, b: integer): integer;
begin
   Sum := a + b;
end;

function Sum(a, b: integer): integer;
begin
   result := a + b;
end;

And this code will not be compiled:

function Sum(a, b: integer): integer;
var
   result: integer;
begin
   result := a + b;
   Sum := result;
end;

The distinctions of the compiler as compared to other 32-bit Pascal compilers

It is useful to know some features of the compiler used on the server.

  • There are no modules crt and wincrt on the server because they contain procedures that are not needed for solving problems.
  • Dynamic memory allocation involves rounding the size of a block to a multiple of 16 bytes. For example, if you allocate a lot of blocks 4 bytes each, then your program will use four times more memory than you really need. Use static data structures, which are free from this drawback and work faster than dynamic structures.
  • Sets can occupy either 4 or 32 bytes, which also may lead to a significant memory overuse. In many cases a good alternative to sets is using bits of an integer (or of an array of integers).
  • It is forbidden to convert explicitly the types of integer and floating-point variables. For example, the expression x := extended(i), where the type of x is extended, and i is integer, is incorrect, whereas the expression x := i is valid.

It is forbidden to change the value of the variable of a for loop inside the loop. The following code will not be compiled:

for i := 1 to 10 do
begin
   if i mod 2 = 0 then inc(i);
   writeln(i);
end;

Moreover, after a for loop the value of the loop variable is not defined. Therefore, this value cannot be used, for example, for a search. The following example will work differently with different compilers.

var
   a: array[1..10] of integer;
   i: integer;
begin
   for i:=1 to 10 do
      a[i] := i*i;
   for i:=1 to 10 do
      if a[i] = 17 then break;
   writeln('i = ', i);
end.
{
   Delphi output:      i = 11
   FreePascal output:  i = 10
}

How to use 64-bit integer data types

The compiler fully supports 64-bit integers (both signed and unsigned). A signed 64-bit integer ranges from –9223372036854775808 to 9223372036854775807 and an unsigned 64-bit integer ranges from 0 to 18446744073709551615. The signed type is called int64, and the unsigned type is called qword. The following example illustrates the use of 64-bit integer types:

var
   a: int64;
   b: qword;
begin
   read(a);
   read(b);
   writeln(a);
   writeln(b);
end.

How to read input data until the end of stream

You should know how to read the input data until the end of stream to solve certain problems. The following examples show how to implement reading input until the end of stream in case input data consists of numbers, lines or separate characters.

{numbers}
var n: integer;
...
while not seekeof do
begin
   read(n);
   ...
end;

{lines}
var line: string;
...
while not eof do
begin
   readln(line);
   ...
end;

{characters}
var c: char;
...
while not eof do
begin
   read(с);
   ...
end;

Other notes

Sometimes the Wrong Answer verdict actually means Runtime error. This is because FreePascal independently intercepts some runtime errors and sends a message to the output flow.

In order to increase the size of a stack and to avoid its overflow when using a “deep” recursion, you should use a special directive (in the example, the size of the stack is set to be 16 MB):

{$M 16777216}

It is convenient to use the conditional directive ONLINE_JUDGE for debugging solutions:

var
   a, b: longint;
begin
{$IFNDEF ONLINE_JUDGE}
   assign(input, 'input.txt');
   reset(input);
   assign(output, 'output.txt');
   rewrite(output);
{$ENDIF}
   readln(a, b);
   writeln(a + b);
{$IFNDEF ONLINE_JUDGE}
   close(input);
   close(output);
{$ENDIF}
end.

This program will read data from the file input.txt on your computer and output the result to the file output.txt. On the server, it will use the standard input and output flows.

Earlier compilers

  • FreePascal 1.0.6 was used until August 8, 2005.
  • FreePascal 2.0.1 was used until January 4, 2006.
  • FreePascal 2.0.2 was used until October 24, 2006.
  • FreePascal 2.0.4 was used until October 3, 2014.