MRECAMAN - Recaman’s Sequence
Tác giả: khuc_tuan
Ngôn ngữ: Pascal
// {$APPTYPE CONSOLE}
{$mode delphi}
type
Hash = class
x : integer;
n : Hash;
end;
var
table : array[0..1000000] of Hash;
procedure add(x : integer);
var
q : Hash;
begin
q := Hash.Create;
q.x := x;
q.n := table[x mod 999997];
table[x mod 999997] := q;
end;
function exist(x : integer) : boolean;
var
q : Hash;
begin
q := table[x mod 999997];
while q<>nil do
begin
if q.x=x then
begin
exist := true;
exit;
end;
q := q.n;
end;
exist := false;
end;
var
a : array[0..500000] of integer;
x, i : integer;
begin
a[0] := 0;
add(0);
for i:=1 to 500000 do
begin
a[i] := a[i-1] - i;
if (a[i] < 0) or exist(a[i]) then
a[i] := a[i-1] + i;
add(a[i]);
end;
while true do
begin
read(x);
if x=-1 then break;
writeln(a[x]);
end;
end.