[Algorithm] Count the squares

{$MODE OBJFPC}
program SquareCounter;
const
  InputFile  = 'SQUARES.INP';
  OutputFile = 'SQUARES.OUT';
  m = 2013;
var
  fi, fo: TextFile;
  n: Int64;
  x: array[1..3] of Int64;
  i: Integer;
  res: Int64;
begin
  AssignFile(fi, InputFile); Reset(fi);
  AssignFile(fo, OutputFile); Rewrite(fo);
  try
    while not SeekEof(fi) do
      begin
        ReadLn(fi, n);
        x[1] := n; x[2] := n + 1; x[3] := 2 * n + 1;
        for i := 1 to 3 do
          if x[i] mod 2 = 0 then
            begin
              x[i] := x[i] div 2;
              Break;
            end;
        for i := 1 to 3 do
          if x[i] mod 3 = 0 then
            begin
              x[i] := x[i] div 3;
              Break;
            end;
        res := (x[1] mod m) * (x[2] mod m) * (x[3] mod m) mod m;
        WriteLn(fo, res);
      end;
  finally
    CloseFile(fi); CloseFile(fo);
  end;
end.

Because I want you to read the problem and think of a solution, Then I look at my solution and then look at the code, so it's located on many pages and I also believe that you won't want to read the problem and get the solution right away..

Page 4 will talk about a few things and expand on the formula in our article: